.NET Task runs in a single thread

Can I rely on the fact that a Task always executed on a single thread? It can be anything, but it must be the same for the whole body, since I need the Culture stream to be installed correctly.

 Task bind = Task.Factory.StartNew(() => { Thread.CurrentThread.CurrentCulture = culture; // do some asp.net binding stuff with automatic // date formatting gridView.DataSource = table; gridView.DataBind(); } 

If I can’t, is there a parameter or so to get this behavior?

Cheers, Matthias

+6
source share
2 answers

Yes, the task body is executed in one thread, except when you run subtasks or explicitly create other threads (Thread, ThreadPool, etc.).

0
source

I believe that the code specified for a specific task will be executed in one thread, but there may not be continuations. It would be very difficult to write valid code if the tasks were opaque to the flow inside the body of the task, so I am sure that you are fine.

On the other hand, your code can become cleaner if you explicitly use the culture where necessary. This may not be feasible depending on the side of ASP.NET, but it is worth considering if it is possible.

+3
source

Source: https://habr.com/ru/post/891542/


All Articles