2012-03-04 77 views
0

我正在調用線程函數中的類函數。我正在使用invoke方法將值發送到主窗體中的進度條,我不知道如何在線程函數調用的其他類函數中執行此操作。在線程中調用函數調用的信息狀態值

我的目標是從線程函數甚至線程函數調用的函數向進度條發送一個值到主窗體。

我的代碼: 「調用子 公用Sub UpdPgEvent(BYVAL值作爲整數) Me.pgFindEvent.Value =值 結束小組

' Sub started by the thread 
Private Sub ThreadTaskMonitor() 
    Dim ConnectURL As String 

    ' Delegate progressbar 
    Dim DelegPgEvent As DelegueUpgPbEvent = New DelegueUpgPbEvent(AddressOf UpdPgEvent) 

    ' This invoke works great 
    Me.Invoke(DelegPgEvent, 10) 
    ConnectURL = "..." 
    Me.Invoke(DelegPgEvent, 20) 

    ' What I want is to send state value from this call to the main form progressbar 
    urlr.JsonGetEvents(ConnectURL) 
    Dim table_res As List(Of monitor_table) = urlr.ConstructDataMonitor() 
    Me.Invoke(DelegPgEvent, 80) 
    Me.MonitorBindingSource.DataSource = table_res 
    Me.Invoke(DelegPgEvent, 90) 
    mon.dbMonitor.DataSource = Me.MonitorBindingSource 
    Me.Invoke(DelegPgEvent, 0) 
End Sub 

感謝您的幫助

+0

你能修改urlr類嗎?如果是的話,在那裏舉辦活動並在你的調用課堂內處理它。 – nik 2012-03-04 12:47:30

回答

0

您需要將你的主表格傳遞給班級。

下面是一個示例。

'Main Form 
Public Class Form1 

Delegate Sub dlUpdateUI(ByVal sText As String) 


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    Dim t As Thread 

    Dim clsfn As New clsURLfn(Me) 

    t = New Thread(AddressOf clsfn.doit) 
    t.Start() 


End Sub 

Public Sub updateUI(ByVal sText As String) 

    If Me.InvokeRequired Then 

     Me.Invoke(New dlUpdateUI(AddressOf updateUI), sText) 
    Else 

     Me.Text = sText 

    End If 



End Sub 

End Class 'End Main Form 

'Other Class 
Public Class clsURLfn 

Dim Mainfrm As Form1 

Public Sub New(ByRef mainform As Form) 

    Mainfrm = mainform 

End Sub 

Public Sub doit() 

    Mainfrm.updateUI("New Text") 

End Sub 

End Class 'End Other Class 
+0

非常感謝。我做了你告訴我的代碼,它工作正常。 – user1205713 2012-03-04 17:06:27

+0

@ user1205713不客氣!樂意效勞 :) – Himal 2012-03-04 17:21:35