This project has moved and is read-only. For the latest updates, please go here.

beginer's problem, catching errors

Oct 14, 2014 at 5:29 PM
Edited Oct 14, 2014 at 5:31 PM
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()


            var result = _reader.Decode(_luminance);
            if (result != null)
                Dispatcher.BeginInvoke(() => DisplayResult(string.Format("{0} - {1}", result.BarcodeFormat, result.Text)));
                  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)

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;

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

        _photoCamera.AutoFocusCompleted += PhotoCameraAutoFocusCompleted;


Thanks in advance
Oct 14, 2014 at 7: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.