我在線程中運行以下代碼以枚舉活動目錄中的本地計算機。這需要一些時間才能完成(大約5-10秒),因此如果用戶在枚舉完成之前退出應用程序,應用程序需要5-10秒才能退出。我試過thread.abort,但因爲它正在等待For Each SubChildEntry In SubParentEntry.Children
完成它不會中止,直到這返回。我可以立即停止運行.NET的線程:
Dim childEntry As DirectoryEntry = Nothing
Dim ParentEntry As New DirectoryEntry
ParentEntry.Path = "WinNT:"
For Each childEntry In ParentEntry.Children
Windows.Forms.Application.DoEvents()
Select Case childEntry.SchemaClassName
Case "Domain"
Dim SubChildEntry As DirectoryEntry
Dim SubParentEntry As New DirectoryEntry
SubParentEntry.Path = "WinNT://" & childEntry.Name
'The following line takes a long time to complete
'the thread will not abort until this returns
For Each SubChildEntry In SubParentEntry.Children
Select Case SubChildEntry.SchemaClassName
Case "Computer"
_collServers.Add(SubChildEntry.Name.ToUpper)
End Select
Next
End Select
Next
RaiseEvent EnumComplete()
你能告訴我們你用來創建和啓動一個新線程的代碼嗎? – 2011-05-25 11:30:01