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[
^]
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