Как отменить подвешивание нити?
Hello,
i'm interested in how you can cancel hanging thread?
What I have tried:
<pre>Example:
Public Sub FileStreamWrite(ByVal pFs As FileStream, ByVal pByPrint As Byte())
pFs.Write(pByPrint, 0, pByPrint.Length)
End Sub
Dim processThreads As ProcessThreadCollection = Process.GetCurrentProcess.Threads
Dim newThread1 As New Thread(CType(Sub() FileStreamWrite(fs, pByPrint), ThreadStart))
newThread1.IsBackground = True
newThread1.Start()
Do
Application.DoEvents()
If Now.TimeOfDay.Subtract(timeStart).TotalSeconds > 10 Then
Select Case ShowQuestionMessage(MethodBase.GetCurrentMethod(), "Do you want to continue?", "", MessageBoxButtons.RetryCancel, MessageBoxDefaultButton.Button1)
Case DialogResult.Cancel
processThreads = Process.GetCurrentProcess.Threads
Dim a = OpenThread(1, False, CType(processThreads(processThreads.Count - 1).Id, UInteger))
Dim b = GetNativeThreadId(newThread1)
Dim w = OpenThread(1, False, CType(newThread1.ManagedThreadId, UInteger))
TerminateThread(w, 1)
processThreads = Process.GetCurrentProcess.Threads
isRepeat = False
Return
Case Else
timeStart = Now.TimeOfDay
End Select
End If
Loop While newThread1.IsAlive AndAlso isRepeat