Skip to content Skip to sidebar Skip to footer

Convert Asynctask To Task In Monodroid/c#

I was wondering how to implement this Android AsyncTask using .NET's async libraries: public class LoadRecordTask : AsyncTask { private Activity1 _context; private int _rec

Solution 1:

The AsyncTask you are using could be translated to something like:

_progressDialog = ProgressDialog.Show( _context , "", "Loading record {0}", _recordId );

if (await LoadRecord(_recordId))
    _progressDialog.Dismiss();
else
    _progressDialog.SetMessage( "Error loading recording." );

Where LoadRecord could be returning Task<bool> and the internals running inside of Task. Otherwise you can just wrap the LoadRecord method you are currently using in a Task to make it run async.

private Task<bool> LoadRecord(int recordId){
    return Task<bool>.Run(() => 
    {
        //Do stuff here to fetch recordsreturntrue;
    });
}

The method you are calling await from needs to be marked as async. I.e.:

privateasyncvoidMyAwesomeAsyncMethod() {}

Post a Comment for "Convert Asynctask To Task In Monodroid/c#"