Vb.net 线程:如何更新标签或关闭窗体
我以前在线程方面做得不多,更新标签和关闭表单时遇到问题 调试时,CloseDialog子线程肯定在主线程中运行,所以我不明白它为什么不关闭。表单上没有循环或任何其他运行来保持其打开状态。我还遇到了一个问题,即使用从另一个线程实时传递的信息更新标签上的文本 AddOUToTreeView子模块被调用并正常工作,但是来自frmStatus的子模块从不做任何事情 frmMain:Vb.net 线程:如何更新标签或关闭窗体,vb.net,multithreading,Vb.net,Multithreading,我以前在线程方面做得不多,更新标签和关闭表单时遇到问题 调试时,CloseDialog子线程肯定在主线程中运行,所以我不明白它为什么不关闭。表单上没有循环或任何其他运行来保持其打开状态。我还遇到了一个问题,即使用从另一个线程实时传递的信息更新标签上的文本 AddOUToTreeView子模块被调用并正常工作,但是来自frmStatus的子模块从不做任何事情 frmMain: Private WithEvents bkg As New ADSearcher Private Sub startSe
Private WithEvents bkg As New ADSearcher
Private Sub startSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles startSearch.Click
With bkg
.RootPath = "LDAP://domain.com"
.FilterString = "(objectCategory=organizationalUnit)"
If Not integratedAuth Then
.UserID = "user"
.Password = "pass"
End If
.PageSize = 5
.PropertiesToLoad = New String() {"cn", "name", "distinguishedName", "objectCategory"}
Dim search As New Threading.Thread(AddressOf .StartSearch)
search.Start()
Dim statusDialog As frmStatus = New frmStatus
statusDialog.Show() 'I want to use ShowDialog() but removed it when trouble shooting
End With
End Sub
Private Delegate Sub displayStatus(ByVal entriesFound As Integer)
Private Sub bkg_ResultFound(ByVal ousFound As Integer) Handles bkg.ResultFound
Dim display As New displayStatus(AddressOf frmStatus.UpdateOUSearcherStatus)
Me.Invoke(display, New Object() {ousFound})
End Sub
Private Delegate Sub displayResult(ByVal node As TreeNode)
Private Delegate Sub closeStatusDialog()
Private Sub bkg_SearchCompleted(ByVal ouNodes As TreeNode) Handles bkg.SearchCompleted
Dim display As New displayResult(AddressOf AddOUToTreeView)
Me.Invoke(display, New Object() {ouNodes})
Dim closeStatus As New closeStatusDialog(AddressOf frmStatus.CloseDialog)
Me.Invoke(closeStatus)
End Sub
Private Sub AddOUToTreeView(ByVal node As TreeNode)
tvOU.Nodes.Add(node)
tvOU.TopNode.Expand()
End Sub
frmStatus这两个函数都不起任何作用:
Public Sub CloseDialog()
'Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
'Me.Dispose()
End Sub
Public Sub UpdateOUSearcherStatus(ByVal entriesFound As Integer)
'lblOUsFound.Text = Format("{0} Organizational Units Found", ousFound)
lblOUsFound.Text = entriesFound.ToString
End Sub
在我的ADSearcher课程中,我有:
Public Event ResultFound(ByVal ousFound As Integer)
Public Event SearchCompleted(ByVal ouNodes As TreeNode)
并以以下方式提出活动:
RaiseEvent ResultFound(resultCount)
'and
RaiseEvent SearchCompleted(rootNode)
关于这一点有数百个问题,都是同一个问题。在工作线程中使用类型名frmStatus创建表单的新实例。一个你看不见的。但是关闭得很好;您需要statusDialog,它现在是一个局部变量,因此无法访问。改为字段。谢谢您的回复。这是有道理的,哈哈。我认为这是一个愚蠢的错误。那么,我是否应该能够将statusDialog的声明移到startSearch_Click sub之外并调用statusDialog.CloseDialog?我不知道你说的把它变成一个字段是什么意思。非常感谢。