Why my aplication doesn't LoadAndCompleteInstance after some execution using BackgroundWorker?

18 Views Asked by At
using (var bgw = new BackgroundWorker())
                            {
                                bgw.DoWork += delegate
                                {

                                    System.Threading.Thread.Sleep(1000);

                                };
                                bgw.RunWorkerCompleted += async delegate
                                {
                                    SystemInstancesTable savedLine = null;

                                    savedLine = await Communication.CallAsync<SystemInstancesTable>("/api/Agent/GetDurableInstance", MethodEnum.POST, workflowGuid);

                                    //insert into flowster retore points table the restore point

                                    if (savedLine.BlockingBookmarks.StartsWith("FMSActivityApprovalWithValuesBookmarkResumed"))
                                    {
                                        //run LoadAndComplete
                                        await LoadAndCompleteInstance(workflowGuid, parameters.TimestampId, parameters, tenantId, savedLine, sourceFromDesigner, savedLine.BlockingBookmarks, null, pendingWfId);
                                    }
                                    else
                                    {
                                        //run LoadAndComplete
                                        await LoadAndCompleteInstance(workflowGuid, parameters.TimestampId, parameters, tenantId, savedLine, sourceFromDesigner, "", "", pendingWfId: pendingWfId);
                                    }
                                };
                                bgw.RunWorkerAsync();

After some execution my code is not responding anymore. there is any problem whit my code?is the BackgroundWorker busy?

1

There are 1 best solutions below

1
Stephen Cleary On

BackgroundWorker doesn't work well with async code. The modern way to do the same thing is Task.Run; see my blog series for an examination of why Task.Run is better than BackgroundWorker in every scenario.

A Task.Run-based approach would look like this:

await Task.Run(() =>
{
  // (original DoWork code)
  System.Threading.Thread.Sleep(1000);
});
SystemInstancesTable savedLine = null;
savedLine = await Communication.CallAsync<SystemInstancesTable>("/api/Agent/GetDurableInstance", MethodEnum.POST, workflowGuid);

//insert into flowster retore points table the restore point
if (savedLine.BlockingBookmarks.StartsWith("FMSActivityApprovalWithValuesBookmarkResumed"))
{
  //run LoadAndComplete
  await LoadAndCompleteInstance(workflowGuid, parameters.TimestampId, parameters, tenantId, savedLine, sourceFromDesigner, savedLine.BlockingBookmarks, null, pendingWfId);
}
else
{
  //run LoadAndComplete
  await LoadAndCompleteInstance(workflowGuid, parameters.TimestampId, parameters, tenantId, savedLine, sourceFromDesigner, "", "", pendingWfId: pendingWfId);
}