VB .Net 的 多執行緒

研究了半天,終於知道 VB .Net 的多執行緒該怎麼寫了…
不敢獨享…有緣者自然會看得到…
 
    Dim trd As System.Threading.Thread
 
    Private Sub DialogThread_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        trd = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf ThreadProc))
        trd.IsBackground = True
        trd.Start()
    End Sub
 
    Public Sub ThreadProc()
        Try
            Dim mi As New MethodInvoker(AddressOf Me.UpdateProgress)
            Do While Me.ProgressBar1.Value < Me.ProgressBar1.Maximum
                ‘ Call BeginInvoke on the Form
                Me.BeginInvoke(mi)
                System.Threading.Thread.Sleep(100)
            Loop
            ‘ Thrown when the thread is interupted by the main thread – exiting the loop
        Catch e As System.Threading.ThreadInterruptedException
            ‘ Simply exit….
        Catch we As Exception
        Finally
            If ProgressBar1.Value = ProgressBar1.Maximum Then Me.Invoke(New MethodInvoker(AddressOf Me.reset))
        End Try
    End Sub
 
    Private Sub UpdateProgress()
        ‘ Reset to start if required
        ‘If ProgressBar1.Value = ProgressBar1.Maximum Then
        ‘ProgressBar1.Value = ProgressBar1.Minimum
        ‘End If
        ProgressBar1.PerformStep()
        Console.WriteLine(ProgressBar1.Value)
    End Sub
 
    Private Sub reset()
        ProgressBar1.Value = ProgressBar1.Minimum
    End Sub
 
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        trd.Resume()
    End Sub
 
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        trd.Suspend()
    End Sub
 
後記: 1. 想看懂這個程式,至少需要對於 VB .Net 處理 Thread 有概念..
2. 另外….想 Copy 的朋友,請至少問過我一下…
 
廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s