C
C#2y ago
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);
}
}
}
}
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?
5 Replies
qqdev
qqdev2y ago
You are probably dealing with multiple threads here The UI (CardStatusField.Text) can only be updated from the UI thread You can use the invoke mechanism to trigger updates
qqdev
qqdev2y ago
Stack Overflow
Invoke(Delegate)
Can anybody please explain this statement written on this link Invoke(Delegate): Executes the specified delegate on the thread that owns the control's underlying window handle. Can anybody explain
qqdev
qqdev2y ago
^ Example
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
aaronduce
aaronduceOP2y ago
thanks!
Want results from more Discord servers?
Add your server