1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
Imports System.IO
Public Class Frm_Main
Private _InProgress As Boolean
Private _BigFilePath As String = "C:\Windows 8.1 AIO.iso"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Annuler" Then
BackgroundWorker1.CancelAsync()
Else
If Not BackgroundWorker1.IsBusy Then
BackgroundWorker1.RunWorkerAsync(_BigFilePath)
_InProgress = True
Button1.Text = "Annuler"
End If
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim fs As New FileStream(e.Argument.ToString, FileMode.Open)
Dim bytesRead As Integer = 0
Dim bytesSoFar As Long = 0
Dim buffer As Byte() = New Byte(10239) {}
Dim progressPercent As Integer
While (InlineAssignHelper(bytesRead, fs.Read(buffer, 0, buffer.Length))) <> 0
Try
bytesSoFar += bytesRead
progressPercent = (bytesSoFar * 100) / fs.Length
Me.Invoke(New UpdateProgessCallback(AddressOf Me.updateprogress), "Chargement en cours : " & progressPercent.ToString & " %")
BackgroundWorker1.ReportProgress(progressPercent, Nothing)
If BackgroundWorker1.CancellationPending Then
BackgroundWorker1.ReportProgress(0)
fs.Dispose()
GC.Collect()
e.Cancel = True
Exit Sub
End If
Catch ex As Exception
fs.Dispose()
GC.Collect()
Exit Sub
End Try
End While
fs.Close()
End Sub
Private Delegate Sub UpdateProgessCallback(ByVal txt$)
Private Sub updateprogress(ByVal txt$)
Me.Text = txt
End Sub
Public Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If Not e.ProgressPercentage > 100 Then
ProgressBar1.Value = e.ProgressPercentage
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
MsgBox("Opération annulée !")
Button1.Text = "Lancer"
Me.ProgressBar1.Value = 0
Me.Text = "ReadBigFile"
Else
MsgBox("Opération terminée !")
Button1.Text = "Lancer"
End If
_InProgress = False
End Sub
Private Sub Frm_Main_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If _InProgress Then
MsgBox("Veuillez attendre la fin du chargement ou annuler l'opération !")
e.Cancel = True
End If
End Sub
End Class |
Partager