C#C
C#3y ago
7 replies
aaronduce

Cross Thread operation not valid: control 'CardStatusField' acessed from a thread other than the thr

namespace Project1
{
    public partial class Form1 : Form
    {
        NFCReader NFC = new NFCReader();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            NFC.Connect(); // Connecting to the reader

            ConnectedReaderField.Text = NFC.GetReadersList()[0].ToString(); // this sets the 'connected reader' input. NFC.GetReadersList is a LIST object containing connected ACS compatible readers

            NFC.CardInserted += new NFCReader.CardEventHandler(CardInsertedAction);

            NFC.Watch();
        }

        public void CardInsertedAction()
        {
            try
            {
                if (NFC.Connect())
                {
                    CardStatusField.Text = "Present -> CARD UID: " + NFC.GetCardUID();
                } else 
                {
                    CardStatusField.Text = "Failed to connect";
                }
            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}


The issue occurs in the function CardInsertedAction, when running CardStatusField.Text - .........

Can i bring this into the context of just running on new NFCReader.CardEventHandler? It wants an Object in this.

Or can i combat it within this function?
Was this page helpful?