How to correctly abort a running Matlab function/thread in a .NET Assembly in .NET8?
Dear MathWorks team
In our .NET Framework 4.7.2 application we instanciate a class from a Matlab .NET assembly and call one function to calculate.
The call is done in a own background thread of the application.
If the calculation takes too long or the calculation request has been canceled, we just aborted the .NET thread with thread.Abort().
.NET core (at least v8) does not support the Abort() function anymore. The only general alternative suggestion I could find is to outsource the call into an own process and then kill the process if required. This seems to be quite complicated and Matlab takes a few seconds to initialize on the first call, which is too slow for us.
Is there any best practice / example how to achieve this?
Thanks in advance
Public Sub New(ByVal theTimeout As Integer, theData As Object, theWorkerMethod As TheWorkerDelegate, theCompleteCallback As CompleteCallbackDelegate, theSynchObj As ISynchronizeInvoke)
_theData = theData
_theCompleteCallback = theCompleteCallback
_theWorkerMethod = theWorkerMethod
_theSyncObject = theSynchObj
If theTimeout > 0 Then
_timeoutTimer = New WcsTimer(theTimeout * 1000, WcsTimer.TimerMode.SingleShot)
AddHandler _timeoutTimer.TimeElapsed, AddressOf _timeoutTimerElapsed
End If
_theThread = New Threading.Thread(AddressOf _startWorkerThread)
_theThread.Name = "Matlab Supervision Thread"
_theThread.Start()
End Sub
Public Sub Abort()
_stopTimeoutTimer()
If _theThread IsNot Nothing AndAlso _theThread.IsAlive Then
_theThread.Abort()
End If
_theThread = Nothing
End Sub
Private Sub _startWorkerThread()
Dim theEx As Exception = Nothing
Try
_theWorkerMethod(_theData) -> Matlab call
Catch ex As Exception
theEx = ex
End Try
_stopTimeoutTimer()
Dim p As Object() = {_theData, False, theEx}
_theSyncObject.Invoke(_theCompleteCallback, p)
End Sub
Private Sub _timeoutTimerElapsed(sender As Object, e As EventArgs)
WcsTrace.Log(WcsTrace.Category.Detailed, $"********* BackgroupWorker Timeoute -> Abort **************")
Abort()
_theCompleteCallback(_theData, True, Nothing)
End SubDear MathWorks team
In our .NET Framework 4.7.2 application we instanciate a class from a Matlab .NET assembly and call one function to calculate.
The call is done in a own background thread of the application.
If the calculation takes too long or the calculation request has been canceled, we just aborted the .NET thread with thread.Abort().
.NET core (at least v8) does not support the Abort() function anymore. The only general alternative suggestion I could find is to outsource the call into an own process and then kill the process if required. This seems to be quite complicated and Matlab takes a few seconds to initialize on the first call, which is too slow for us.
Is there any best practice / example how to achieve this?
Thanks in advance
Public Sub New(ByVal theTimeout As Integer, theData As Object, theWorkerMethod As TheWorkerDelegate, theCompleteCallback As CompleteCallbackDelegate, theSynchObj As ISynchronizeInvoke)
_theData = theData
_theCompleteCallback = theCompleteCallback
_theWorkerMethod = theWorkerMethod
_theSyncObject = theSynchObj
If theTimeout > 0 Then
_timeoutTimer = New WcsTimer(theTimeout * 1000, WcsTimer.TimerMode.SingleShot)
AddHandler _timeoutTimer.TimeElapsed, AddressOf _timeoutTimerElapsed
End If
_theThread = New Threading.Thread(AddressOf _startWorkerThread)
_theThread.Name = "Matlab Supervision Thread"
_theThread.Start()
End Sub
Public Sub Abort()
_stopTimeoutTimer()
If _theThread IsNot Nothing AndAlso _theThread.IsAlive Then
_theThread.Abort()
End If
_theThread = Nothing
End Sub
Private Sub _startWorkerThread()
Dim theEx As Exception = Nothing
Try
_theWorkerMethod(_theData) -> Matlab call
Catch ex As Exception
theEx = ex
End Try
_stopTimeoutTimer()
Dim p As Object() = {_theData, False, theEx}
_theSyncObject.Invoke(_theCompleteCallback, p)
End Sub
Private Sub _timeoutTimerElapsed(sender As Object, e As EventArgs)
WcsTrace.Log(WcsTrace.Category.Detailed, $"********* BackgroupWorker Timeoute -> Abort **************")
Abort()
_theCompleteCallback(_theData, True, Nothing)
End Sub Dear MathWorks team
In our .NET Framework 4.7.2 application we instanciate a class from a Matlab .NET assembly and call one function to calculate.
The call is done in a own background thread of the application.
If the calculation takes too long or the calculation request has been canceled, we just aborted the .NET thread with thread.Abort().
.NET core (at least v8) does not support the Abort() function anymore. The only general alternative suggestion I could find is to outsource the call into an own process and then kill the process if required. This seems to be quite complicated and Matlab takes a few seconds to initialize on the first call, which is too slow for us.
Is there any best practice / example how to achieve this?
Thanks in advance
Public Sub New(ByVal theTimeout As Integer, theData As Object, theWorkerMethod As TheWorkerDelegate, theCompleteCallback As CompleteCallbackDelegate, theSynchObj As ISynchronizeInvoke)
_theData = theData
_theCompleteCallback = theCompleteCallback
_theWorkerMethod = theWorkerMethod
_theSyncObject = theSynchObj
If theTimeout > 0 Then
_timeoutTimer = New WcsTimer(theTimeout * 1000, WcsTimer.TimerMode.SingleShot)
AddHandler _timeoutTimer.TimeElapsed, AddressOf _timeoutTimerElapsed
End If
_theThread = New Threading.Thread(AddressOf _startWorkerThread)
_theThread.Name = "Matlab Supervision Thread"
_theThread.Start()
End Sub
Public Sub Abort()
_stopTimeoutTimer()
If _theThread IsNot Nothing AndAlso _theThread.IsAlive Then
_theThread.Abort()
End If
_theThread = Nothing
End Sub
Private Sub _startWorkerThread()
Dim theEx As Exception = Nothing
Try
_theWorkerMethod(_theData) -> Matlab call
Catch ex As Exception
theEx = ex
End Try
_stopTimeoutTimer()
Dim p As Object() = {_theData, False, theEx}
_theSyncObject.Invoke(_theCompleteCallback, p)
End Sub
Private Sub _timeoutTimerElapsed(sender As Object, e As EventArgs)
WcsTrace.Log(WcsTrace.Category.Detailed, $"********* BackgroupWorker Timeoute -> Abort **************")
Abort()
_theCompleteCallback(_theData, True, Nothing)
End Sub .net8, thread, abort MATLAB Answers — New Questions