Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to convert long running procedure to asynchronous

RonNYCRonNYC Member ✭✭✭

My app contains a procedure which could be long-running. I would like to give users the opportunity to stop it.

The procedure is:

void longRunningProcedure()
(it does not access the file system or the Internet)

And within it, once certain variables are initialized, it calls doIt();

I tried this:

async void longRunningProcedure()

and within this procedure, I have tried:

await Task.Run(() => doIt());

and have tried:

await Task.Run(async () => await doIt());

But when the code reaches this point I get this error:

The selected debug engine does not support any code executing on the current thread (e.g. only native runtime code is executing).

Unhandled Exception:

Android.Util.AndroidRuntimeException: occurred
The long-running code doesn't even run at all.

What to do?



  • DarioOrgazDarioOrgaz USMember ✭✭

    Try this:
    Task longRunningProcedure(){};
    main() { CancellationToken cancellationToken = new CancellationToken(); Task.Run(longRunningProcedure, cancellationToken); }

  • RonNYCRonNYC Member ✭✭✭
    edited March 2019

    FIRST, let me thank everyone here who replied to my questions. Sites like this (and StackOverflow) really cut dev time by 90%. I am very grateful for everyone's comments. So after some research etc, this is what I did:

    I changed the void longRunningProcedure() to async void, like below:

     async void runLongRunningProcedure()
            await Task.Run(() => actualProcedure());

    for my app, the actualProcedure() is just:

        void actualProcedure()

    Now, apart from this code, I have a variable,

    bool cancelPressed

    which starts off as false

    I have a button and a menu item which sets this to true when pushed or clicked.

    And this all works. I can kick off the procedure and stop it with the button or menu click.

    There was another issue: The long running procedure writes data to one of the app's text boxes.

    Doing that resulted in this error:

    The selected debug engine does not support any code executing on the current thread (e.g. only native runtime code is executing).

    SO, I had to modify the data writing section to this:

     RunOnUiThread( () => {
                        txtBox.Text += modifiedData.ToString();

    modifiedData is a StringBuilder type of variable.

    And, this all works.

    At the appropriate moment during the procedure I have:

     if (cancelPressed == true) return

    Just to be safe, I enclosed this in try/catch.

    Of course if I wanted to notify the user that the procedure was cancelled (or anything else while it is running) via a Toast message I have to do this:

    RunOnUiThread(() => {

    I have a procedure called MakeToast which handles the message formatting, etc.

    It was simpler than I thought it would be but all of it is due to the help I was given here and StackOverflow, even suggestions which didn't work out.


Sign In or Register to comment.