2012年5月7日 星期一

Q: How to implement Multi-threading?
A: 
1. Simple

Module Module1

    Sub Main()
        For i As Integer = 1 To 10
            ' create a thread and run
            Dim obj As New Threading.Thread(AddressOf ThreadMethod)
            obj.Start()
        Next
        Console.ReadLine()
    End Sub

    ' the running method in every thread
    Sub ThreadMethod()
        Console.WriteLine(Now)
    End Sub
End Module
2. With Parameters : add Sub() before the method in Thread object initialization
Dim obj As New Thread(Sub() SaveImage(url, mFileName))
obj.Start()

...

    Public Sub SaveImage(url As String, mFileName As String)
        'Public Sub SaveImage()
        'Dim url As String, mFileName As String
        Dim objWebClient As New WebClient

        objWebClient.DownloadFile(url, mFileName)
        Console.WriteLine(url)
    End Sub