gpt4 book ai didi

.net - Thread.abort永远挂起

转载 作者:行者123 更新时间:2023-12-03 13:21:30 25 4
gpt4 key购买 nike

在远程计算机上启动wmi查询的代码。
此代码同时在多个线程中运行。

oquery = New ManagementObjectSearcher("select * from Win32_Computersystem")
oquery.Options.ReturnImmediately = True
oquery.Options.Rewindable = False
oquery.Options.Timeout = New TimeSpan(0, 8, 0)
oquery.Scope = mycomputer.omsWMI 'timeout of omswmi is set to (0,1,0)

For Each objMgmt In oquery.Get 'on some pesky windows XP machines this line hangs forever regardless of the timeout and the thread can never be used to get info from another computer. the only way to "release" this is to reboot the target computer.
'do stuff
next

在主线程中运行的代码:
Public Sub KillLongRunningThreads()
Dim tel As Integer
SyncLock LockMythreadlist
For tel = MyThreadlist.Count - 1 To 0 Step -1
If CType(MyThreadlist(tel), wmithread).Starttime < Now.AddMinutes(-120) Then
DBG("INFO: before threadabort")
Try
'this line tries to abort the first thread but is also hanging forever, the INFO:after threadabort is never reached.
'even worse this line hangs in the middle of a synclock which causes the program to stop working (all other threads are waiting for the end syncklock.
CType(MyThreadlist(tel), wmithread).TheThread.Abort()
Catch ex As Exception
DBG(ex.Message & ex.StackTrace)
End Try
DBG("INFO: after threadabort")
MyThreadlist.RemoveAt(tel)
End If
Next
End SyncLock
End sub

两个问题:

如何停止第一个线程?

如何停止第一个线程而不创建死锁?

最佳答案

Thread.Abort的文档中:

f Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.



由于您的线程似乎卡在非托管代码中,因此中止托管线程将无济于事。

我不知道如何防止线程卡在WMI查询中,或者如何取消挂起的WMI查询。

关于.net - Thread.abort永远挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7435426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com