Can you show a UI Alert on the UI from a non UI thread/code file

Hi All,

Maybe I am going about this the wrong way (being a newbie and all) but I have started to create an Android app where data is loaded from several xml files to a SQLite Database when the App is started and this is working fine (after a bit of help in another thread).

The loading of these files is done in a different thread to the UI thread. Here is the code that I use to start the thread (and the progressdialog works, which I am so proud of myself in getting to work):

            ProgressDialog progressDialog = ProgressDialog.Show(this, "", "Creating/Loading Database...", true);
            progressDialog.Indeterminate = true;
            new Thread(new ThreadStart(delegate
                //Start Database Update Processes

                //HIDE PROGRESS DIALOG
                RunOnUiThread(() => progressDialog.Dismiss()); 

If there is an error, I want to show an Alert box but this is where I am coming undone.

I can run the Alert box on the UI thread, but obviously, when running the code in the other thread, I cant seem to get the Alert to show on the UI thread as I don't have any reference to it (well, I don't seem to have although as in the above code, I thought I was passing the reference by the work this).

So, I suppose the questions are:
1) Can you have an alert box show on the current running UI from a different thread (I'm presuming you should be able to if you have the references)
2) If so, how do you get the reference to the thread (ie: do you pass the reference to the main class like I did, or is there another way).
3) If not, what would be a suggestion to capture any errors and show them (at the moment, I use a messy string builder, build up the string with errors, if any, then display an alert if the string is not equal to OK. I didn't show that code above, but the alert code, that works, is represented by the ........ above)

Any help greatly appreciated



  • RofiqSetiawanRofiqSetiawan USMember ✭✭✭

    maybe these code could help

    Toast.MakeText (Application.Context, "Adele : Hello from the other thread...", ToastLength.Short).Show ();
  • RobertBrown.2438RobertBrown.2438 USMember

    Thanks for the response, however, I am running into the same problem when I thought I got the alert box code to work.

    I get the error:
    Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    I did try and research the Looper.prepare() previously but could not get a definitive answer.

    Just for the sake of it (from what I read), I tried the following code.

    Toast.MakeText(Application.Context, message, ToastLength.Short).Show();

    and amazing it did show the Toast, but then, it was stuck in the loop and I didn't have anyway to tell it to stop the loop (even when the Toast finished displaying the message.

Sign In or Register to comment.