MessagingCenter and queuing work

ChizzChizz Member ✭✭

Greetings

I'm in the process of building a small app that makes use of a barcode scanner to get information about articles we make from a sql database that i have setup. I'm currently implementing a version using the work if this gentlemen here: https_://acaliaro.wordpress.com/2017/02/07/connect-a-barcode-reader-to-a-xamarin-forms-app-via-bluetooth/

I have several pages in myapp that each subscribe / unsubscribe to the barcode message when the page appears / dissapears like this:

protected override void OnAppearing()
        {
            base.OnAppearing();
            MessagingCenter.Subscribe<App, string>(this, "Barcode", (sender, arg) =>
            {
                arg = arg.Replace("\r\n", string.Empty);
                if (arg != "")
                {
                    Device.BeginInvokeOnMainThread(async () =>
                    {
            //Invoking on main thread to be able to update UI elements
                        await Barcode(arg);
                    });
                }
            });
        }

private async Task Barcode(string input)
        {
    //Getting data async from a webservice
}

My problem is that as soon as the user scans a new barcode the app will begin to process the Barcode method again even though the first instance has not finished yet which can cause all sorts of problems if the user is too fast for the app, how can i make sure that each barcode the user scans in gets processed after eachother?

Best Regards

Posts

  • WasserEsserWasserEsser Member ✭✭

    Very primitive but you could create a simple queue where every scanned barcode has to line itself into. Your callback then checks if the queue is empty. If it is, it can start processing, if not, wait a bit and check again until its empty.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @WasserEsser is right.
    Or you can simply add a boolean flag

    Something like this (not tested...)

    bool _isBarcode = false;
    protected override void OnAppearing()
            {
                base.OnAppearing();
                MessagingCenter.Subscribe<App, string>(this, "Barcode", (sender, arg) =>
                {
    
    
                    arg = arg.Replace("\r\n", string.Empty);
                    if (arg != "")
                    {
                        Device.BeginInvokeOnMainThread(async () =>
                        {
                //Invoking on main thread to be able to update UI elements
    
                if(_isBarcode)
                    return;
    
                _isBarcode = true;
    
    
                            await Barcode(arg);
    
                _isBarcode = false;
    
                        });
                    }
                });
            }
    
  • ChizzChizz Member ✭✭
    edited July 2018

    Thanks for your suggestions, i ended up with this for now, not sure if i can make a better version but it works atleast.

    private List<BarcodeList> _barcodelist;
    
    public MovePage()
    {
       InitializeComponent();
        _barcodelist = new List<BarcodeList>();
    }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            _isTimerEnabled = true;
            Device.StartTimer(TimeSpan.FromMilliseconds(50), () =>
            {
                if (_barcodelist.Count > 0 && !_isProcessing)
                {
                    _isProcessing = true;
                    Barcode(_barcodelist[0].Barcode);
                    _barcodelist.RemoveAt(0);
                }
    
                return _isTimerEnabled;
            });
    
            MessagingCenter.Subscribe<App, string>(this, "Barcode", (sender, arg) =>
            {
                arg = arg.Replace("\r\n", string.Empty);
                if (arg != "")
                {       
                    BarcodeList barcode = new BarcodeList
                    {
                        Barcode = arg
                    };
                    _barcodelist.Add(barcode);
                }
            });
        }
    
    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        MessagingCenter.Unsubscribe<App, string>(this, "Barcode");
        _isTimerEnabled = false;
    }
    
    private async void Barcode(string input)
    {
        //Do stuff here
        _isProcessing = false;
    }
    
Sign In or Register to comment.