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 86 87 88 89 90 91 92 93 94 95 96 97 98
|
'***********CLASS GLOBAL****************************
Imports System.Threading
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.IO
Public Delegate Sub Method1CompletedEventHandler(ByVal sender As Object, ByVal e As MediaListCompletedEventArgs)
Public Delegate Sub ProgressChangedEventHandler(ByVal e As MediaListProgressChangedEventArgs)
Public Class MediaListStep
'delegate Worker asynchronous
Private Delegate Sub WorkerEventHandler(ByVal message As String, ByVal asyncOp As AsyncOperation)
'delegate posts
Private onCompletedDelegate As SendOrPostCallback
Private onProgressReportDelegate As SendOrPostCallback
'Events for the main thread.
Public Event Method1Completed As Method1CompletedEventHandler
Public Event ProgressChanged As ProgressChangedEventHandler
Public Sub New()
InitializeDelegates()
End Sub
Protected Overridable Sub InitializeDelegates()
onProgressReportDelegate =
New SendOrPostCallback(AddressOf ReportProgressFunc)
onCompletedDelegate =
New SendOrPostCallback(AddressOf CompletedDelegateFunc)
End Sub
' function called by SendOrPostCallback to raise Method1Completed Event
Private Sub CompletedDelegateFunc(ByVal state As Object)
Dim e As MediaListCompletedEventArgs = state
OnCompleted(e)
End Sub
Protected Sub OnCompleted(ByVal state As Object)
Dim e As MediaListCompletedEventArgs = state
RaiseEvent Method1Completed(Me, e)
End Sub
' function called by SendOrPostCallback to raise ProgressChanged Event
Private Sub ReportProgressFunc(ByVal state As Object)
Dim e As ProgressChangedEventArgs = state
OnProgressChanged(e)
End Sub
Protected Sub OnProgressChanged(ByVal e As ProgressChangedEventArgs)
RaiseEvent ProgressChanged(e)
End Sub
'Synchrnous version of the method
Public Sub Method1(ByVal message As String)
For i As Integer = 1 To 100000
'Do some time consuming process
Next
End Sub
'Asynchoronous version of the method
Public Sub Method1Async(ByVal message As String, ByVal userState As Object)
Dim asyncOp As AsyncOperation = AsyncOperationManager.CreateOperation(userState)
Dim worker As WorkerEventHandler = New WorkerEventHandler(AddressOf Method1Worker)
'Execute process Asynchronously
worker.BeginInvoke(message, asyncOp, Nothing, Nothing)
End Sub
'This method does the actual work
Private Sub Method1Worker(ByVal message As String, ByVal asyncOp As AsyncOperation)
Dim progressArgs As MediaListProgressChangedEventArgs = Nothing
Dim dir As DirectoryInfo = New DirectoryInfo("C:\Users\OUNIS\Desktop")
Dim files As IEnumerable(Of FileInfo) = Dir.EnumerateFiles()
Dim N As Integer = 0
Dim VideoFiles = New List(Of VideoFile)
For Each file In files
'Do some time consuming process
VideoFiles.Add(New VideoFile(file.Name, file.Length))
progressArgs = New MediaListProgressChangedEventArgs(N,
CSng(N / VideoFiles.Count) * 100, asyncOp.UserSuppliedState)
asyncOp.Post(onProgressReportDelegate, progressArgs)
N += 1
' Yield the rest of this time slice.
Thread.Sleep(0)
Next
Dim completArgs As MediaListCompletedEventArgs = New MediaListCompletedEventArgs(
VideoFiles.Count, VideoFiles,
Nothing, False, asyncOp.UserSuppliedState)
asyncOp.PostOperationCompleted(onCompletedDelegate, completArgs)
End Sub
End Class |
Partager