Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2007, 07:26 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Obvious Oversite?

This problem has to do with updating a control/dataset based on the DataRecevied event of the serial port. Now I know you are thinking cross threading (which it might be) but let me explain.

I have a dataset in the main UI form that is databound to a number of controls. When I change the values of the dataset the bound controls update. This is fine, but when I update the values of the dataset via the DataReceived event the values DON'T update? To illlustrate the problem simply I have stripped down the code to it's most basic form. I hope someone can shed some light on the problem.

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // create a dataset and populate it with data
            UnitDataSet uds = new UnitDataSet();
            uds.DIns.ReadXml("Data.xml");

            // create a new comms instance and pass a ref to the dataset
            _comms = new Comms(uds);

            // create a label to bind too
            Label lblInputState = new Label();
            panel1.Controls.Add(lblInputState);

            // databind the control to the state column
            lblInputState.DataBindings.Add("Text", uds.Tables["DIns"], "state");
        }

        // used to test if the binding works and value changes as expected
        public void button1_Click(object sender, EventArgs e)
        {
            _comms.ChangeValue();
        }
    }

================================================== ==========

class Comms
    {
        SerialPort _sp;
        UnitDataSet _uds;

        public Comms(UnitDataSet uds)
        {
            _uds = uds;
            _sp = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One);
            _sp.DataReceived += new SerialDataReceivedEventHandler(_sp_DataReceived);
            _sp.Open();
        }

        private void _sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // run this method when the event fires
            ChangeValue();
        }

        // if i call this from the main form button (see above ) the value changes but if it's called
        // by the above DataReceived event the value doesn't change!?!
        public void ChangeValue()
        {
            // simple toggle to change the value
            bool tmp = (bool)_uds.Tables["DIns"].Rows[0]["state"];
            _uds.Tables["DIns"].Rows[0]["state"] = !tmp;
        }
    }


 
Old February 14th, 2007, 08:52 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not so sure if it's an obvious oversite, but the problem was if it's run in debug mode in the IDE it doesn't work but the built version without the IDE works fine. Thanks to Ed Poore who found out what was happening!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Am I missing the obvious?? crazeydazey XSLT 4 August 4th, 2006 03:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.