I got a financial library (com) that call an event each time a price is change. I subscribed to this event and try to update a textbox, but I always get a cross-thread operation. Fine, I use delegate, but it’s doesn’t work at all… it’s freeze the application. My hypothesis is that the event is call to quickly, before the GUI have time to refresh. Any idea how I could deal with that ?
The only way I resolve it for now it to put that :
CheckForIllegalCrossThreadCalls = false;
but I know it’s a very bad practice …
I use .Net 3.5 with c#
EDIT : Delegate code
private delegate void UpdateTextBoxDelegate(String value); private void UpdateTextBox(String value) { this.txtPrice.Text = value; } txtPrice.Invoke(new UpdateTextBoxDelegate(UpdateTextBox), price);
If you are using events, it should marshal over to the UI thread automatically. That’s how
BackgroundWorkerworks. You will have to post how you hook into the event and the event callback itself.You can also try using
InvokewhenInvokeRequired == true.So now you can use
InvokeExon any ISynchronizeInvoke and be able to access the properties and fields of implementing class.