none
Supplying variables to a new task RRS feed

  • Question

  • I often use the following basic structure for starting a parallel task when I don't
    need to wait for the result

    Private sub yyy()

    Dim SUB_VAR1 as string = xxx
    Dim SUB_Var2 as integer = xxx

    Dim T1 as new task(sub)
    Dim TASK_VAR1 as String = SUB_VAR1
    Dim TASK_VAR2 as Integer = SUB_VAR2
    ...
    ... 'do some work within the task and the TASK_Variable(s)
    ...
    ...
    End Sub)

    T1.Start()

    End sub
    No problem there - but here is my question

    Is it guaranteed that my SUB_variable values ALWAYS get transferred into the Task_VARIABLES before I start the task or does this happen afterwards (I hope not :-)) ... If it would happen afterwards, the Sub_variables might not be present anymore and I would get exceptions ...

    PS I'm just trying to understand how this actually get's processed
    Thursday, March 7, 2019 4:36 AM

All replies

  • Hi,
    if the sub (your sub yyy) will be executed in one thread you must put a copy of your variables in a object (boxing) and start the task. If your variables are reference-types you must put a deep copy in the parameter object. If your sub yyy reentered in multi-threading you must use synclock.

    In the following demo you can see the incorrect results in the Sub2:

    Imports System.Threading
    
    Module Module02
    
      Sub Main()
        Try
          Dim c As New Demo
          c.Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
      Friend Class Demo
        Friend Sub Execute()
          For i = 1 To 5
            Dim arg As Object = i
            Dim t = New TaskFactory().StartNew(New Action(Of Object)(AddressOf Sub1), arg, CancellationToken.None)
          Next
          Console.WriteLine(" ---")
          Dim d2 As New Demo2
          For i = 11 To 15
            d2.ID = i
            Dim arg As Object = d2
            Dim t = New TaskFactory().StartNew(New Action(Of Object)(AddressOf Sub2), arg, CancellationToken.None)
          Next
          Console.WriteLine(" ---")
          Dim d3 As New Demo3
          For i = 101 To 105
            d3.ID = i
            Dim arg As Object = d3.Copy
            Dim t = New TaskFactory().StartNew(New Action(Of Object)(AddressOf Sub3), arg, CancellationToken.None)
          Next
        End Sub
    
        Private Sub Sub1(par As Object)
          Thread.Sleep(100)
          Console.WriteLine($"Sub1: {par}")
        End Sub
        Private Sub Sub2(par As Object)
          Thread.Sleep(100)
          Dim o = TryCast(par, Demo2)
          Console.WriteLine($"Sub2: {o.ID}")
        End Sub
        Private Sub Sub3(par As Object)
          Thread.Sleep(100)
          Dim o = TryCast(par, Demo3)
          Console.WriteLine($"Sub3: {o.ID}")
        End Sub
      End Class
    
      Friend Class Demo2
        Friend Property ID As Integer
      End Class
    
      Friend Class Demo3
        Friend Property ID As Integer
        Friend Function Copy() As Demo3
          Dim c As New Demo3
          c.ID = Me.ID
          Return c
        End Function
      End Class
    End Module
    


    --
    Viele Grüsse / Best Regards
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Thursday, March 7, 2019 5:56 AM