I have a Backgroundworker whose purpose is to complete tasks sequentially in the background. Now one task is performed in a multithreaded way. This means that Backgroundworker will create multiple threads. I use a parallel task library, so I use Task.Factory.StartNew to create multiple tasks.
After completing the tasks, the Phonoverter waits for their completion.
Now I print ManagedThreadID Backgroundworker and all managed tasks ManagedThreadID. I found that BackgroundWorker ManagedThreadID always matches the first ManagedThreadID task. I think this should not happen, so I canβt explain. I think the Backgroundworker thread should be different from all the tasks it creates, so ManagedThreadID should be different from each other.
Can someone explain why this scenario is happening? Thank you very much.
Edit:
The code is similar to this:
Backgroundworker.Run(){
var task = Task.Factory.StartNew(action1);
taskList.Add(task);
task = Task.Factory.StartNew(action2);
taskList.Add(task);
...
foreach(var task in taskList) task.Wait();
}
You will find that one task has the same ManagedThreadID as Backgroundworker.
Steve source
share