2012-03-04 27 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