Test PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End ClassTest PWE le 26/07/2023 Imports System.IO Imports System.Net Imports System.Threading Imports System.ComponentModel Imports EurekaSolutions.LiveUpdate2019.Core Friend Class AsyncDownloadForm Implements IAsyncDownloadForm 'rivate _Instance As WebClient Private _WebClient As WebClient Private _EvtCompleted As New ManualResetEvent(False) Private _CancelAsked As Boolean Private _Cancelled As Boolean Private _Error As Exception Private _Uri As Uri Private _LocalFileName As String ' Date de début du transfert, pour afficher le temps nécessaire estimé Private _StartAt As DateTime ' Nombre d'octets restant à transférer... Private _RemainingDatas As Long Public Function DoUpload(ByVal pWebClient As WebClient, ByVal pLocalFileName As String, ByVal pUri As Uri) As Boolean Implements IAsyncDownloadForm.DoUpload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient 'If pUri.Segments.Count > 0 Then ' Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1) 'Else Me.lbFichier.Text = pUri.OriginalString 'End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback ' Specify a progress notification handler. AddHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback 'OD20110228 "STOR" forcé car la doc MS est ambigue sur la valeur par défaut ! pWebClient.UploadFileAsync(pUri, "STOR", pLocalFileName) 'methode = "STOR" pour FTP et POST pour HTTP ! Do If _EvtCompleted.WaitOne(500) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler pWebClient.UploadFileCompleted, AddressOf UploadCompletedCallback RemoveHandler pWebClient.UploadProgressChanged, AddressOf UploadProgressCallback If _Error IsNot Nothing Then Throw _Error End If If _Cancelled Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu par l'utilisateur") End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Public Function DoDownload(ByVal pWebClient As WebClient, ByVal pUri As Uri, ByVal pLocalFileName As String) As Boolean Implements IAsyncDownloadForm.DoDownload ProgressBarControl2.Position = 1 btnCancel.Enabled = True _CancelAsked = False _Cancelled = False _Error = Nothing lbProgression.Text = "" _RemainingDatas = 99999L _WebClient = pWebClient If pUri.Segments.Count > 0 Then Me.lbFichier.Text = "Fichier : " & pUri.Segments(pUri.Segments.Count - 1).Replace("%20", " ") Else Me.lbFichier.Text = pUri.OriginalString End If _Uri = pUri _LocalFileName = pLocalFileName _StartAt = DateTime.Now Try Me.Show() _EvtCompleted.Reset() ' Specify that the DownloadFileCallback method gets called ' when the download completes. AddHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback ' Specify a progress notification handler. AddHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback _WebClient.DownloadFileAsync(pUri, pLocalFileName, Me) Do If _EvtCompleted.WaitOne(1000) Then Exit Do End If Application.DoEvents() Loop While True Application.DoEvents() Catch ex As Exception _Cancelled = True _Error = ex _EvtCompleted.Set() End Try Me.Hide() RemoveHandler _WebClient.DownloadFileCompleted, AddressOf DownloadCompletedCallback RemoveHandler _WebClient.DownloadProgressChanged, AddressOf DownloadProgressCallback Try If _Cancelled Then If File.Exists(pLocalFileName) Then File.Delete(pLocalFileName) End If End If Catch End Try If _Error IsNot Nothing Then Throw _Error End If If _RemainingDatas > 0L Then Throw New Exception("Upload de [" & pUri.OriginalString & "] interrompu alors qu'il restait " & _RemainingDatas & " octets à transférer.") End If Return (Not _Cancelled) End Function Private Sub DownloadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New AsyncCompletedEventHandler(AddressOf DownloadCompletedCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If ProgressBarControl2.Position = 100 btnCancel.Enabled = False If e IsNot Nothing Then _Cancelled = e.Cancelled _Error = e.Error End If _EvtCompleted.Set() _RemainingDatas = 0L 'If Not _Cancelled Then ' Dim ts As TimeSpan = DateTime.Now.Subtract(_StartAt) ' lbProgression.Text = String.Format("{0} bytes reçus sur {1} ({2} %)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage) 'End If End Sub Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New DownloadProgressChangedEventHandler(AddressOf DownloadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" _RemainingDatas = e.TotalBytesToReceive - e.BytesReceived If e.ProgressPercentage > 1 AndAlso e.BytesReceived > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesReceived Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If Dim wtxtrate As String = "" If wts.TotalSeconds > 2 AndAlso e.BytesReceived > 0 Then wtxtrate = String.Format(" - taux moyen: {0} Ko/seconde", Convert.ToInt32(e.BytesReceived / 1024 / wts.TotalSeconds)) End If ProgressBarControl2.Position = e.ProgressPercentage lbProgression.Text = String.Format("{0:### ### ###} Ko reçus sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesReceived \ 1024), (e.TotalBytesToReceive \ 1024), e.ProgressPercentage, sreste) & wtxtrate '_Cancelled = e. '_Error = e.Error End Sub Private Sub UploadCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) DownloadCompletedCallback(sender, e) End Sub Private Sub UploadProgressCallback(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs) 'Attention au multi threading !! If Me.InvokeRequired Then Dim lme As New UploadProgressChangedEventHandler(AddressOf UploadProgressCallback) Me.Invoke(lme, New Object() {sender, e}) Return End If If _CancelAsked Then _WebClient.CancelAsync() End If Dim wts As TimeSpan = DateTime.Now.Subtract(_StartAt) Dim wrests As Double = 0 Dim sreste As String = "" 'OD --> e.ProgressPercentage semble foireux !! _RemainingDatas = e.TotalBytesToSend - e.BytesSent Dim wprogressp100 As Int32 = Convert.ToInt32(Math.Floor(e.BytesSent * 100 / e.TotalBytesToSend)) If wprogressp100 > 1 AndAlso e.BytesSent > 0 Then wrests = wts.TotalSeconds * _RemainingDatas / e.BytesSent Dim mn As Int32 = Convert.ToInt32(Math.Floor(wrests / 60)) Dim sec As Int32 = Convert.ToInt32(Math.Floor(wrests - mn * 60)) If mn > 0 Then sreste = String.Format(" - Temps estimé restant: {0} mn {1} secondes", mn, sec) Else sreste = String.Format(" - Temps estimé restant: {0} secondes", sec) End If End If ProgressBarControl2.Position = wprogressp100 lbProgression.Text = String.Format("{0:### ### ###} Ko envoyés sur {1:### ### ###} Ko ({2} %) {3}", (e.BytesSent \ 1024), (e.TotalBytesToSend \ 1024), wprogressp100, sreste) If _RemainingDatas = 0 Then 'Pour forcer la main dans les cas où ca ne s'arrête pas tout seul... UploadCompletedCallback(sender, Nothing) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click btnCancel.Enabled = False _CancelAsked = True _Cancelled = True ProgressBarControl2.Position = 0 lbProgression.Text = "* * * * Transfert annulé * * * *" End Sub Public ReadOnly Property RemainingDatas() As Long Implements IAsyncDownloadForm.RemainingDatas Get Return _RemainingDatas End Get End Property End Class