0

I'm using a Motorola scanner for a WPF application and it seems that anytime I use the scanner to interact with the UI I get some kind of multithreading issue.

For example:

internal class Scanner
{
    ...//other functions create string from barcode
    ...//constructor creates scan event that calls OnBarCodeEvent

    public delegate void EventHandler();
    public event EventHandler ScanEvent = delegate { };

    public string strBarcode { get; set; }

    public void OnBarcodeEvent(short eventType, ref string scanData)
    {
        strBarcode = GetBarcodeFromXml(scanData);

        ScanEvent();
    }
}

public partial class MainWindow : Window
{
    private Scanner scanner;

    public MainWindow()
    {
        InitializeComponent();

        scanner = new Scanner();
        scanner.ScanEvent += ScanEvent;//create an event for the scanner, ScanEvent() was added 
    }

    public void ScanEvent()
    {
        var strBarcode = scanner.strBarcode;//gets barcode string from Scanner class

        //this is just one example of how I use it
        this.Dispatcher.Invoke(new Action(() => { tbUserName.Text = strBarcode; }));
    }
}

Now the problem is anytime I want to interact with the UI with anything that I got from the scan event, I have to use this.Dispatcher.Invoke(new Action(() => {...} ));

Anytime that I do anything with the scanner and UI without the Dipatcher.Invoke, I get the error The calling thread cannot access this object because a different thread owns it.

I would like to know if there is a better way to handle the scan event or is this the only way I am going to be able to use the scanner to interact with the UI?

Note: the example I showed is only ONE little example, I use the scanner in all aspects of my application so I'd like not to use the Dispatcher.Invoke.

  • Closed. See my answer in the duplicated question. – Federico Berasategui Jun 23 '14 at 14:32
  • [Here](http://stackoverflow.com/questions/21883735/prevent-using-dispatcher-invoke-in-wpf-code/21884638#21884638) is another answer that might help you, though much of it is the same.. – Federico Berasategui Jun 23 '14 at 14:58
  • Thank you, that was very insightful. But one thing that kind of goes along these lines, if I changed the ViewModel how would I get immediate results? For example if I change a variable then check some values on a webserver, as of now the text in a textbox that is bound to the UI wont change until the call is complete. Sometimes there is a little bit of a delay in the webservice call so I'd like the UI to change instantly. Is that possible? – thestephenstanton Jun 23 '14 at 16:51
  • the call to the WebService should be performed in a background thread, whereas with my solution the UI update (via DataBinding) is performed in the UI thread... these two threads are separate and will not bother each other. the UI thread is free to do the UI stuff while the other thread performs the Web Service call – Federico Berasategui Jun 23 '14 at 16:58

0 Answers0