This project has moved. For the latest updates, please go here.

beginer's problem, catching errors

Oct 14, 2014 at 4:29 PM
Edited Oct 14, 2014 at 4:31 PM
Hi
I've download the windowsphone example in order to star understandng and start using that as base code for my App.

My basic problem is when I try to set a text to my textblock when ZXing can not decode an image, in that moment there is a runtime error,... "Invalid cross-thread access."

The code is writen in the codebehind, not mvvm , just plain and simple codebehind
    private string ScanPreviewBuffer()
    {
        try
        {

            _photoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);

            var result = _reader.Decode(_luminance);
            if (result != null)
            {
                Dispatcher.BeginInvoke(() => DisplayResult(string.Format("{0} - {1}", result.BarcodeFormat, result.Text)));
            }
            else
            {
                  txtError.Text="ERROR read again"; //<< HERE  JUMP THE ERROR
            }

        }
        catch (Exception error)
        {
            return error.Message.ToString();
        }
        return "ok";

}

The ScanPreviewBuffer function is called from :
    void PhotoCameraAutoFocusCompleted(object sender, CameraOperationCompletedEventArgs e)
    {
        ScanPreviewBuffer();
    }

The PhotoCameraAutoFocusCompleted handler is asociated to event,.. like this:
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        this._photoCamera = new PhotoCamera();
        _photoCamera.Initialized += OnPhotoCameraInitialized;
        _previewVideo.SetSource(_photoCamera);

        CameraButtons.ShutterKeyHalfPressed += (o, arg) => _photoCamera.Focus();

        _photoCamera.AutoFocusCompleted += PhotoCameraAutoFocusCompleted;

        base.OnNavigatedTo(e);
    }


Thanks in advance
Coordinator
Oct 14, 2014 at 6:18 PM
You have to do it the same way like in the line above:
Dispatcher.BeginInvoke(() => DisplayResult("ERROR read again"));
Don't access gui controls from a background thread. Use the Dispatcher instead.