在BackgroundWorker常用的方法中有兩個方法提供了可以傳遞狀態物件的多載.
(1) BackgroundWorker.RunWorkerAsync (Object) 多載方法:此一方法提供傳遞狀態物件到DoWork事件中DoWorkEventArgs.Argument屬性值。
Dim CycleCount As Integer = 1
BackgroundWorker1.RunWorkerAsync(CycleCount)
呼叫RunWorkerAsync時將CycleCount變數傳遞給DoWork事件
(2) BackgroundWorker.ReportProgress (Int32, Object) 多載方法:此一方法提供傳遞狀態物件到ProgressChanged事件中的ProgressChangedEventArgs.UserState屬性值。
Public Class StateObject '自訂一個要傳送內容的類別
Public i As Integer
Public CycleCount As Integer
Sub New()
i = 0
CycleCount = 0
End Sub
End Class
BackgroundWorker1.ReportProgress(i, CycleObject)