How to call alert dialog in timer class?

In my application, I'm trying to call my custom alert dialog in timer.elapsed but i cant able to show the alert since its prompting me an error like as follows.

{Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2970/46c3f7e0/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00063] in /Users/builder/data/lanes/2970/46c3f7e0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:178
at Android.App.AlertDialog+Builder.Create () [0x00043] in /Users/builder/data/lanes/2970/46c3f7e0/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.AlertDialog.cs:173
at TabletEvalLibrary.AndroidUtility.aTimer_Elapsed (System.Object sender, System.Timers.ElapsedEventArgs e) [0x0000f] in c:\Session Mangament\OnBoard Mobile\OnBoard Mobile 1.9\Tablet Evaluation Core\TabletEvalLibrary\TabletEvalLibrary\AndroidUtility.cs:288
--- End of managed exception stack trace ---
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:200)
at android.os.Handler.(Handler.java:114)
at android.app.Dialog.(Dialog.java:113)
at android.app.AlertDialog.(AlertDialog.java:125)
at android.app.AlertDialog$Builder.create(AlertDialog.java:968)
}

Here is my code :

public static void SetTimer()
{
// Create a timer with a two second interval.

        aTimer.Interval = 6000;
        aTimer.Enabled = true;
        aTimer.Start();
        aTimer.Elapsed += aTimer_Elapsed;
        //aTimer.AutoReset = true;

    }

 static void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
           Logout(logcon, logsession, logmessage, null, null, logtiltle);
    }

 {
        try
        {

            Handler mainHandler = new Handler(context.MainLooper);
           // Looper.Prepare();
            Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(context);
            AlertDialog alert = builder.Create();
            alert.SetIcon(Android.Resource.Drawable.IcDialogAlert);
            if (message == null)
            {
                alert.SetMessage("Are you sure you want to logout?");
                yesButtonText = "Yes";
                noButtonText = "No";
                alert.SetTitle("Logout");
            }
            else
            {
                alert.SetMessage(message);
                alert.SetTitle(title);
            }
            alert.SetButton(yesButtonText, (s, ev) =>
            {
                ((Activity)context).Finish();
                login.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
                context.StartActivity(login);
            });
            alert.SetButton2(noButtonText, (s, ev) =>
            {
                //stay on screen
            });
            alert.Show();
        }
        catch(Java.Lang.Exception ex)
        {
            throw ex;
        }
    }
Sign In or Register to comment.