我正在開發工作程序,用戶可以在AD中搜索某個計算機的資產標籤。如果發現它會給他們一個列表框中的列表。我擁有所有這些工作,但是當他們進行搜索時,UI會凍結。對於這個問題,我是VB和OO的新手。我知道它是凍結的,因爲搜索運行在與用戶界面相同的線程上,但我不能爲我的生活獲得另一個線程來完成這項工作。當我嘗試在另一個線程中執行搜索時,我無法更新列表框,因爲它不在同一個線程中。任何幫助將不勝感激。VB.Net線程化和保存結果
功能,搜索AD:
Private Function searchAd()
'clear the results from previous entries
' AdResultListBox.Items.Clear()
Try
Dim rootEntry As New DirectoryEntry("GC://mydomaininfo")
Dim searcher As New DirectorySearcher(rootEntry)
'selects the Computer Name property
searcher.PropertiesToLoad.Add("cn")
Dim compname As String = PropertyTagTextbox.Text
'searches using wildcards
compname = "*" + compname + "*"
searcher.Filter = "(&(name=" + compname + ")(objectcategory=moreADinformation))"
Dim results As SearchResultCollection
results = searcher.FindAll()
Dim result As SearchResult
For Each result In results
'this is the part i'm having trouble with
Me.AdResultListBox.Items.Add(result.Properties("cn")(0)
Next
Catch ex As Exception
End Try
End Function
Private Sub ADSearchButton_Click(sender As Object, e As RoutedEventArgs) Handles ADSearchButton.Click
AdResultListBox.Items.Clear()
'create the new thread for searching
Dim SearchThread As New Thread(AddressOf searchAd)
SearchThread.Start()
End Sub
我沒有看到他們的任何線程代碼 – logixologist
我拿出來,因爲它沒有工作。完全一樣。 –