I'm developing some control software for a robot in Visual Basic and I cannot for the life of me figure out why the execution of a procedure, which works flawlessly outside of the Task
framework, does not work when I try to run it concurrently to the rest of the code. I'm trying to use the TPL to let a certain procedure run in the background while allowing the user to use the GUI in parallel (currently, the program freezes during execution of this procedure, and isn't active again until it's over, and this can take hours). The problem here that I couldn't solve by looking at other questions is, my NullReferenceException happens inside the Task area, but if I don't try to run it concurrently to the rest of the software, the exception never, ever happens. This is the code I'm using:
Dim taskSPME = Task(Of Boolean).Factory.StartNew(Function()
Return MainForm.startSPME((SPMEpanel.SPMECoordinates), SPMEpanel.WaitingTimes, SPMEpanel.WellVolumes, SPMEpanel.StirPosition, SPMEpanel.StirringSpeed, SPMEpanel.StirringRadius, SPMEpanel.HoverZ, SPMEpanel.GridScaling, SPMEpanel.IncrementWithin)
End Function)
Try
taskSPME.Wait()
MsgBox("Task return value: " & taskSPME.Result.ToString)
Catch ex As AggregateException
For Each fault In ex.InnerExceptions
If TypeOf (fault) Is InvalidOperationException Then
MsgBox("Invalid Operation Exception")
ElseIf TypeOf (fault) Is NullReferenceException Then
MsgBox("Null Reference Exception. " & fault.Message.ToString)
ElseIf TypeOf (fault) Is IO.FileNotFoundException Then
MsgBox("File not found Exception")
End If
Next
End Try
startSPME()
, located in another GUI class called MainForm
, is a function that runs fine and returns a boolean value if it's not called from any sort of parallel/concurrent framework. I've tried using Parallel.Invoke()
and even the old ThreadPool
stuff, and they all just throw the same exception, which happens to be the NullReferenceException
where the fault message says "Object reference not set to an instance of an object"
. I tried performing trivial tasks inside the StartNew()
argument, like a MsgBox
for example, and those run fine. The imports are
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Threading.Thread
Can someone tell me what I'm forgetting here? Are there conditions for the task called by the StartNew()
constructor to perform? Like, can it not be located in another class? Originally, I didn't even want to call the Wait()
method on my task, since I want the program to go back to the GUI while the robot performs its experiments, but since nothing happens to the robot unless the startSPME()
function is outside of that parallel task code, I have no idea what's going wrong here.