I have an algorithm which is executted in task from tpl.
From each iteration I want to execute an event handler which update the progress bar in winforms client (throught mvp preseter)
public delegate void NotifyAboutIterationEnd(int iteration);
public event NotifyAboutIterationEnd Notify;
var task = Task.Factory.StartNew(() =>
{
foreach (..
{
var t = Task.Factory.StartNew(p =>
{
Notify(++index);
},CancellationToken.None,TaskCreationOptions.None,Scheduler);
foreach (..
The problem is that, in debug mode I see first is executing the code from the main task. When this code is over, then debugger goes to the inner task. How can I synchronize it? I want to update the progress bar in each iteration. Now the effect is that, I have algorithm result in datagridview already and then I see working (updateing) progress bar :/
I assume you’re
Schedulervariable is theTaskSchedulerinstance for the UI synchronization context and so you’re just trying to be consistent by using the Task API isntead of just usingControl/Window::Invokedirectly?The only way to synchronize would be to call
t.Wait(). This will obviously block your parallel work from continuing until the call toNotifycompletes, but if you really need the UI to be synchronized with the progress then this is the only way.