【问题标题】:Raise information state value from a function call in a thread从线程中的函数调用中提高信息状态值
【发布时间】:2012-03-04 08:48:50
【问题描述】:

我在线程函数中调用一个类函数。我正在使用调用方法将值发送到主窗体中的进度条,但我不知道如何在线程函数调用的其他类函数中执行此操作。

我的目标是从线程函数甚至从线程函数调用的函数向主窗体发送一个值到进度条。

我的代码: '调用子 Public Sub UpdPgEvent(ByVal value As Integer) 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

感谢您的帮助

【问题讨论】:

  • 你能修改urlr类吗?如果是这样,则在那里引发事件并在您的调用类中处理它。

标签: vb.net multithreading invoke


【解决方案1】:

您需要将主表单传递给班级。

这是一个示例。

'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

【讨论】:

  • 非常感谢。我在我的代码中做了你告诉我的,它工作正常。
猜你喜欢
  • 2012-02-18
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
  • 2020-03-31
  • 1970-01-01
相关资源
最近更新 更多