AlertDialog Problem

Hello;
I am trying to use the AlertDialog with a custom keyboard. My software uses a scanner that reads barcodes. When I depress the hard trigger, the dialog freezes and becomes unresponsive. Eventually you get a popup that says to wait or terminate the program.

When pausing in the debugger I see that It is stopped on the call to the Looper.Loop() method used to pump messages. Is there a way to fail safe this so that the looper does not lock up the UI?

Thank you!!

Best Answer

  • pdrozdpdrozd ✭✭
    Accepted Answer

    @AlessandroCaliaro said:
    when does the eventhandler called? When you receive a barcode? You have to block because you would not to read more barcordes?

    Thank you Alessandro;
    It was a threading issue with the Dialog Method being called from the thread. It created weird effects. Once removed Everything worked on.

    Appreciate it!!!

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think you should post some code
  • pdrozdpdrozd Member ✭✭
    edited October 2018

    @AlessandroCaliaro said:
    I think you should post some code

        public string UserInputDialog(string caption, string message)
        {
            string selectedInput = string.Empty;
            Looper.Prepare();
    
            try
            {
                var inflater = LayoutInflater.From(CurrentActivity);
                v = inflater.Inflate(Resource.Layout.AlertLayout, null);
                AlertTitleField = v.FindViewById<TextView>(Resource.Id.AlertTitleField);
                newDialog = new NewAlertDialog(CurrentActivity);
                newDialog.Builder.SetView(v, 0, 0, 0, 0);
                Caption = caption;
                Message = message;
                newDialog.CreateDialog();
                newDialog.Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
                newDialog.OnNewShow += OnNewShow;
                newDialog.SetCancelable(false);
                newDialog.Dialog.Show();
            }
            catch (Exception)
            {
            }
            try
            {
                Looper.Loop();        <===== ******* Hangs here
            }
            catch (Exception)
            {
            }
            return selectedInput;
        }
    

    Hangs in the looper when my scanner that is attached to the MainActivity has a hard trigger depress.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    edited October 2018
    Why do you use Looper? I don’t understand what are you doing
  • pdrozdpdrozd Member ✭✭

    @AlessandroCaliaro said:
    Why do you use Looper? I don’t understand what are you doing

    I created a custom dialog for work which I launch from an even handler. The dialog has to block the event handler thread while operations are done on the custom dialog. When I exit, the dialog, I release the event handler. am running the dialog in a task spawned in that event handler.

    here is an example

    public void UiEventHander(object sender, EventArgs e)
    {
    var YesNoDialog = new AlertDialogClass(CurrentActivity);
    Task.Run(() =>
    {
    YesNoDialog.OkDialog("Exception", msg);
    });
    YesNoDialog.WaitOnDialog();

    // DO other stuff
    

    }

    I use the looper in the OkDialog Call to make sure the dialog is processing messages. I did not know how to block the event handler that is instantiating the custom dialog

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    when does the eventhandler called? When you receive a barcode? You have to block because you would not to read more barcordes?

  • pdrozdpdrozd Member ✭✭
    Accepted Answer

    @AlessandroCaliaro said:
    when does the eventhandler called? When you receive a barcode? You have to block because you would not to read more barcordes?

    Thank you Alessandro;
    It was a threading issue with the Dialog Method being called from the thread. It created weird effects. Once removed Everything worked on.

    Appreciate it!!!

Sign In or Register to comment.