Error: Error while recieving data. Message: The calling thread cannot access this object because a

k365
i am using c#.net framework 4.5
using the sample when i init ticker, it shows ticks in debug window. But when I try to access its value to display to a form or something, it gives error...
Error: Error while recieving data. Message: The calling thread cannot access this object because a different thread owns it.

i tried to call a delegate ontick event but still the problem remains same.

main thing is i am calling ticker from from gives this error, or when i call a delegate and points to form, gives error. BUT when i call to print in debug window, it prints well. what is the matter? let me know
  • k365
    private void onTick(Tick TickData)
    {
    Debug.WriteLine("T: Tick " + Utils.JsonSerialize(TickData));

    Tick t = TickData;
    decimal a = t.LastPrice;
    decimal b = t.AveragePrice;
    txt.Text = a + " " + b;

    }

    from above code, when i use debug.writeline, it works good.
    but when i try to get result in textbox or anywhere, it gives error. why ?
  • k365
    any genius to reply ?
  • kiranMaya
    kiranMaya edited July 2019
    @k365
    your calling "txt" from another thread,assume,you were using windows forms ," txt " belongs to UI thread ,you need to use like
    public static void InvokeOnUi(Control control, Action action)
    {
    if (control.InvokeRequired)
    {
    control.BeginInvoke(action);
    }
    else
    {
    action.Invoke();
    }
    }
    //useage
    private void onTick(Tick TickData)
    {
    Debug.WriteLine("T: Tick " + Utils.JsonSerialize(TickData));

    Tick t = TickData;
    decimal a = t.LastPrice;
    decimal b = t.AveragePrice;

    InvokeOnUi(this, () =>
    {
    txt.Text = a + " " + b;

    });
    }
Sign In or Register to comment.