开发者

Thread.abort hangs forever

Code to launch a wmi query on remote computers. This code runs in several threads at the same time.

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

Code running in the main thread:

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.开发者_StackOverflow社区RemoveAt(tel)
                End If
            Next
    End SyncLock
End sub

Two questions:

How to stop the first thread?

How to stop the first thread and not create a deadlock?


In the documentation for 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.

Since your thread appears to be hanging in unmanaged code, aborting the managed thread isn't going to do anything.

I have no idea how to prevent the thread from hanging in the WMI query, or how to cancel the WMI query that's hanging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜