tags:

views:

53

answers:

2

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);
A: 

Can you post the code for updating the texbox?

Igor Brejc
A: 

If you are using events, it should marshal over to the UI thread automatically. That's how BackgroundWorker works. You will have to post how you hook into the event and the event callback itself.

You can also try using Invoke when InvokeRequired == true.

using System.ComponentModel;
public static class ISynchronizeInvokeExtensions
{
  public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
  {
    if (@this.InvokeRequired)
    {
      @this.Invoke(action, new object[] { @this });
    }
    else
    {
      action(@this);
    }
  }
}

So now you can use InvokeEx on any ISynchronizeInvoke and be able to access the properties and fields of implementing class.

this.InvokeEx(f => f.txtPrice.Text = price);
Samuel