Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 16-Jul-17 21:42pm

There's no need for you to use P/Invoke calls - the Thread class has an Abort method.

But you shouldn't use that either: How To Stop a Thread in .NET (and Why Thread.Abort is Evil)[^]

It would be much better to use an Async method, combined with a CancellationToken:
Asynchronous Programming with Async and Await (Visual Basic) | Microsoft Docs[^]
Cancellation in Managed Threads | Microsoft Docs[^]

VB.NET
Public Function FileStreamWrite(ByVal pFs As Stream, ByVal pByPrint As Byte(), ByVal cancellationToken As CancellationToken) As Task
    Return pFs.WriteAsync(pByPrint, 0, pByPrint.Length, cancellationToken)
End Function

Using cts As New CancellationTokenSource()
    Dim task As Task = FileStreamWrite(fs, pByPrint, cts.Token)
    Dim pollPeriod As TimeSpan = TimeSpan.FromSeconds(10)
    
    While Not task.Wait(pollPeriod)
        Select Case ShowQuestionMessage(...)
            Case DialogResult.Cancel
                cts.Cancel()
                task.Wait()
        End Select
    End While
End Using
 
Share this answer
 
v2
Hello,

I tested yours and we are waiting for the task.Wait () also. But I want to stop this task in this case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900