Forum Xamarin.Forms
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 do I prevent an event from

I have an event called LeadCamera_FrameReceived. If the event is fired and the data is not null, I would like the Device.BeginInvokeOnMainThread to not fire again. I do not want a thread to wait. I would want it to just return without doing anything. I tried using a SemaphoreSlim but I had unexpected results. I'll try again while I wait on the community and come back and explain those results.

` private void LeadCamera_FrameReceived(Leadtools.Camera.Xamarin.FrameHandlerEventArgs e)
{
try
{
if (e.Image != null)
{
var data = barcodeEngine.Reader.ReadBarcode(e.Image, Leadtools.LeadRect.Empty, Leadtools.Barcode.BarcodeSymbology.Datamatrix);

                    if (data != null && data.Value != null)
                    {

                    Device.BeginInvokeOnMainThread(() =>
                     {
                         // Do Something on the UI Thread
                     });
                }

            }
        }
        catch (Exception)
        {
            throw;
        }
    }`

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    I would like the Device.BeginInvokeOnMainThread to not fire again.

    Do you mean that the Device.BeginInvokeOnMainThread method will only be called once? If so, add an attribute to set the condition.

    bool IsFired;
    private void LeadCamera_FrameReceived(Leadtools.Camera.Xamarin.FrameHandlerEventArgs e)
    {
        try
        {
            if (e.Image != null)
            {
                var data = barcodeEngine.Reader.ReadBarcode(e.Image, Leadtools.LeadRect.Empty, Leadtools.Barcode.BarcodeSymbology.Datamatrix);
    
                if (data != null && data.Value != null)
                {
                    if (!IsFired)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            // Do Something on the UI Thread
                        });
                    }
                    else
                    {
                        return;
                    }               
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    
Sign In or Register to comment.