I'm creating a new task to grab some information using this line of code:
Task.Factory.StartNew(Sub() ReadOPCData(TimerToRead), TaskCreationOptions.AttachedToParent)
It works but the variable being passed to the subroutine (TimerToRead) seems to get changed by the routine that created the task ... and then the value output within the task ends up being wrong.
If I don't use a task and simply call it as a subroutine it works exactly as expected. The results output are correct and the variable doesn't get changed. However I want to use a task because the calling routine is time sensitive so I want to use other threads as much as possible so I can return to processing my clock ticks and keeping them accurate.
Here is some code from the called routine that isn't working when it's called as a task: (This is just testing code AddErrItem will actually display errors in the future, right now it's just an output location)
DispText = "Timer: " & CurTimerToRead.ToString
If Me.InvokeRequired Then
Me.Invoke(System.Delegate.CreateDelegate(GetType(myInvokedMethod), Me, "AddErrItem"), New Object() {DispText})
Else
AddErrItem(DispText)
End If
Any suggestions greatfully appreciated. I'm very new to multithreaded programming and fairly new to VB.Net