aaronduce
aaronduce
CC#
Created by aaronduce on 2/24/2023 in #help
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);
}
}
}
}
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?
8 replies