0

Because i want to add an rotating 3D cube i decided to switch from Winforms to WPF. I heard it is much easier to implement a 3D graphic there. So i copied my code to the new WPF project. But now i got already problems to visualize my values on the Textboxes. It doesnt work. It looks like the Evenhandler did not fire an event while receiving Data from the com port. I tested my Functions already and they worked!

namespace cube
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            getAvailablePorts();
            serial.DataReceived += Serial_DataReceived;
        }

        public bool button3clicked = false;
        public bool button4clicked = false;
        public bool button5clicked = false;        
        SerialPort serial = new SerialPort();

        void getAvailablePorts()
        {            
            List<string> Itemlist = new List<string>();
            String[] ports = SerialPort.GetPortNames();
            Itemlist.AddRange(ports);
            comboBox1.ItemsSource = Itemlist;            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "" || textBox6.Text == "")
                {
                    MessageBox.Show("Please Select Port Settings");
                }
                else
                {
                    serial.PortName = comboBox1.Text;
                    serial.BaudRate = Convert.ToInt32(textBox6.Text); 
                    serial.Parity = Parity.None;
                    serial.StopBits = StopBits.One;
                    serial.DataBits = 8;
                    serial.Handshake = Handshake.None;
                    serial.Open();
                    MessageBox.Show("connected!");
                }
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Unauthorised Access");
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            MessageBox.Show("connection closed!");
            serial.Close();
        }

        private void AppendText(TextBox tB1, TextBox tB2, TextBox tB3, TextBox tB4, string[] text)
        {
            //try
            {
                tB1.Text = text[0];
                tB2.Text = text[1];
                tB3.Text = text[2];
                tB4.Text = text[3];
            }
            //catch (Exception) { }
        }

        private void Safe_Position1(TextBox tBr1, TextBox tBi1, TextBox tBj1, TextBox tBk1, string[] text)
        {
            if (button3clicked == true)
            {               
                    tBr1.Text = text[0];
                    tBi1.Text = text[1];
                    tBj1.Text = text[2];
                    tBk1.Text = text[3];
                    button3clicked = false;
            }
        }

        private void Safe_Position2(TextBox tBr2, TextBox tBi2, TextBox tBj2, TextBox tBk2, string[] text)
        {
            if (button4clicked == true)
            {                           
                    tBr2.Text = text[0];
                    tBi2.Text = text[1];
                    tBj2.Text = text[2];
                    tBk2.Text = text[3];
                    button4clicked = false;               
            }
        }

        private void Safe_Position3(TextBox tBr3, TextBox tBi3, TextBox tBj3, TextBox tBk3, string[] text)
        {
            if (button5clicked == true)
            {              
                    tBr3.Text = text[0];
                    tBi3.Text = text[1];
                    tBj3.Text = text[2];
                    tBk3.Text = text[3];
                    button5clicked = false;             
            }
        }

        private void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string serialData = serial.ReadLine();
            String[] text = serialData.Split(new char[] { ' ' });
            AppendText(textBox1, textBox2, textBox3, textBox4, text);
            Safe_Position1(textBox5, textBox7, textBox8, textBox9, text);
            Safe_Position2(textBox10, textBox11, textBox12, textBox13, text);
            Safe_Position3(textBox14, textBox15, textBox16, textBox17, text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3clicked = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4clicked = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button5clicked = true;
        }


    }
}

It throws:

The calling thread cannot access this object because a different thread owns it

Has someone a solution for that?

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • 1
    "Evenhandler did not fire an event while receiving Data from the com port" - do you mean `Serial_DataReceived` event handler or some other? `serial.DataReceived` isn't platform dependent. maybe there is some exception and empty `catch {}` ignores it – ASh May 29 '18 at 14:27
  • Oh yes you are right. i got an Exception on that. it says: The calling thread cannot access this object because a different thread owns it. – Walter Nazarenus May 29 '18 at 15:04
  • In my Winforms project i solved it like that if (textBox1.InvokeRequired) { textBox1.BeginInvoke(new Action(() => AppendText(tB1, tB2, tB3, tB4, text))); } – Walter Nazarenus May 29 '18 at 15:17
  • 2
    wpf equivalent is `Dispatcher.Invoke()` – ASh May 29 '18 at 16:38
  • Possible duplicate of [The calling thread cannot access this object because a different thread owns it](https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it) – Bizhan May 30 '18 at 01:28

0 Answers0