-2

Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll Additional information: Impossibile accedere all'oggetto dal thread chiamante perché tale oggetto è di proprietà di un altro thread.

public partial class MainWindow : Window
{
    int baudRate { get; }
    Parity parity { get; }
    int dataBits { get; }
    StopBits stopBits { get; }
    Handshake handshake { get; }
    string portName { get; }
    MainWindow mainWindow;
    SerialPort _serialPort { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        //var conf = new ConfigScanner(this);
        var conf_file_path = string.Format("{0}scannerCOMconf.ini", AppDomain.CurrentDomain.BaseDirectory);
        if (!File.Exists(conf_file_path)) return;
        string[] fileConf = File.ReadAllLines(conf_file_path);
        portName = fileConf[0];
        baudRate = Convert.ToInt32(fileConf[1]);
        parity = (Parity)Enum.Parse(typeof(Parity), fileConf[2]);
        dataBits = Convert.ToInt32(fileConf[3]);
        stopBits = (StopBits)Enum.Parse(typeof(StopBits), fileConf[4]);
        handshake = (Handshake)Enum.Parse(typeof(Handshake), fileConf[5]);
        Open();
    }


    public bool Open()
    {
        _serialPort = new SerialPort();
        try
        {
            _serialPort.BaudRate = baudRate;
            _serialPort.DataBits = dataBits;
            _serialPort.Handshake = handshake;
            _serialPort.Parity = parity;
            _serialPort.PortName = portName;
            _serialPort.StopBits = stopBits;
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(scanBarcode);
            _serialPort.Open();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return false;
        }
        return true;
    }

    void scanBarcode(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();            
        Console.Write(indata);
        /*
         Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll
         Additional information: 
         Impossibile accedere all'oggetto dal thread chiamante 
         perché tale oggetto è di proprietà di un altro thread.*/
        textbox1.Text = indata;
    }
}
ArK
  • 18,824
  • 63
  • 101
  • 135
  • Possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Jim Hewitt Sep 12 '16 at 13:22
  • Take a look at the SerialDataReceivedEventHandler class, https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. You need to refer to the comment above on how to update GUI from Seperate Thread. – Bearcat9425 Sep 12 '16 at 13:23

1 Answers1

0

You can use a delegate to call the GUI stuff in the correct thread. I would do something like:

void scanBarcode(object sender, SerialDataReceivedEventArgs e)
{
    string indata = _serialPort.ReadExisting();            

    this.Invoke((MethodInvoker)delegate
    {
        textbox1.Text = indata;
    }
}
Baddack
  • 1,548
  • 20
  • 30