0

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.

Marcos F.
  • 1
  • 4
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Panagiotis Kanavos Feb 10 '16 at 17:29
  • Where does the NullReferenceException occur? Are the variables there initialized? – Panagiotis Kanavos Feb 10 '16 at 17:30
  • Null check your `ex.InnerExceptions`. You might just have to attach with a debugger and see what's going on. – Cameron Feb 12 '16 at 17:33
  • I was only able to determine that it was a NullReferenceExpetion and the specific message, but I haven't been able to diagnose it further, yet. I just thought the problem might be my usage or some other triviality, since this function doesn't even start to run inside the Task environment, but works flawlessly if I don't try to make it concurrent. I'll get back to you as soon as I have more detailed info (need to fix some hardware issues for now). – Marcos F. Feb 15 '16 at 08:59

0 Answers0