Bug 5021 - BackgroundWorker RunWorkerCompleted does not run within UI thread
Summary: BackgroundWorker RunWorkerCompleted does not run within UI thread
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.1.x
Hardware: PC Windows
: Low normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-05-11 00:05 UTC by Rob
Modified: 2012-06-01 14:33 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Rob 2012-05-11 00:05:39 UTC
attempting to access any UI objects from Main Activity from a BackgroundWorker RunWorkerCompleted event causes an unknown exception. Without accessing UI objects , no exception is thrown. This leads me to believe RunWorkerCompleted is not executed within the UI thread.

public class MainActivity : Activity
{
   private ProgressDialog progressDialog;
   
   protected override void OnCreate(Bundle bundle)
   {
      base.OnCreate(bundle);
      SetContentView(Resource.Layout.init_setup_layout);
      Button searchButton = FindViewById<Button>(Resource.Id.SubmitButton);
      searchButton.Click += new EventHandler(searchButton_Click);
   }
   
   void searchButton_Click(object sender, EventArgs e)
   {
   
    this.progressDialog = ProgressDialog.Show(this, "Search", "Please Wait...");
            
    // find information 
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    worker.RunWorkerAsync();
   }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
           //******** CRASH BELOW ***********//
           this.progressDialog.Hide();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
      // do stuff
    }
}
Comment 1 Jonathan Pryor 2012-05-21 22:39:23 UTC
The problem is that we haven't done any integration between .NET's SynchronizationContext and Android's main thread.

We need to make System.Threading.SynchronizationContext.Current, in effect, return the Android.App.Application.SynchronizationContext instance:

    http://androidapi.xamarin.com/?link=P:Android.App.Application.SynchronizationContext

That will allow (in this case) AsyncOperationManager.SynchronizationContext calls to be invoked on the UI thread, as expected.
Comment 2 Jonathan Pryor 2012-06-01 14:33:18 UTC
Fixed in commit 45ea51bc.