I have a windows mobile application written in C# that has more dialogs. I want to update a dialog, when an event is triggered. Here is the code:
public void ServerStateChanged()
{
// update the interface
try
{
if (this.Focused)
{
this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
}
}
catch (Exception exc)
{
}
}
The code works a few times, but then I get this System.NotSupportedException with this stacktrace: at Microsoft.AGL.Common.MISC.HandleAr()\r\nat System.Windows.Forms.Control.get_Focused()\r\nat DialTester.Communication.TCPServerView.ServerStateChanged()\r\nat ...
Does it matter from which thread is triggered the event? because I can’t figure out what the problem is, why it works a few times and then it crashes.
It’s likely to be a cross-thread issue. Checking
this.InvokeRequiredat the top of the function and reacting accordingly would definitely improve the safety of the function. Something like this: