Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I often use the following basic structure for starting a parallel task when I don't
need to wait for the result

VB
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

What I have tried:

This is more of a generic question for me...
Posted
Updated 27-Mar-19 21:41pm
v2
Comments
Ralf Meier 7-Mar-19 3:35am    
Sorry ... In the moment I don't understand your question and also not your problem.
Please provide your complete Code-part.
According to your actual code : why do you instance your Task inside your method (sub) ?
Graeme_Grant 27-Mar-19 17:38pm    
Looking at your code and reading your question, you're asking about TPL[^] but coding against Threading[^], two totally different ways of doing multitasking. So, you need to provide more code, with a clear description and a clear question on where you are stuck so we can understand better and be able to point you better in the right direction.

1 solution

The answer is simple: "Yes".
And "No".
And "Maybe".

And they are all true at the same time ... it all goes a bit quantum ...

Threading is not simple, if only because it is inherently non-deterministic: you have little or no control over when things happen because the system controls that, subject to the load on the machine and the number of cores you have available to process threads.
Starting a new thread doesn't mean that it runs immediately, it adds it to a "thread pool" of tasks the system needs to do, and when there is a free core and it reaches the top of the list, it will execute. But ... the thread that started the new task is also part of a pool, and Windows is preemptive (which means it will suspend running tasks to give others a chance) so your new thread may run to completion before the Start method returns in the original thread, or it may run at the same time (on a different core) as the original thread code, or it may wait half an hour and then do it's thing.

And you have no significant control over which order is used unless you start coding locks and mutexes etc. to ensure things happen when you expect them to.

So ... given that your SUB_xxx values are local to the yyy method they could exist or not when your Task actually starts to execute.

Instead of relying on luck to get your values in, pass them as parameters to the task and you guarantee the values the Task will process: vb.net - Visual Basic .NET: how to create a task from a method with parameters - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900