You are getting that error because you are attempting to update a control
from another thread.

The best way to handle this is with delegates.  You utilize the
InvokeRequired/BeginInvoke methods to transfer the update request from the
background thread to the UI updater thread.

...Glenn

On Fri, Oct 31, 2008 at 2:19 PM, Dickery1 <[EMAIL PROTECTED]> wrote:

>
> i get a error cross thread operation not valid when it is trying to
> write to the textfield.
>  void appendToTextBox(string str){
>
>
> i have a feeling that the delegates run in a different thread and when
> the events are triggered, they are trying to write to the form thread.
> i guess C# does not like it. how do i fix it.
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
> using Krs.Ats.IBNet;
>
> using Krs.Ats.IBNet.Contracts;
>
> namespace collectTickData
> {
>    public partial class Form1 : Form
>    {
>        private static IBClient client;
>
>        public Form1()
>        {
>            InitializeComponent();
>        }
>        private   void appendToTextBox(string str){
>            textBox1.Text  += str + "\r\n";
>
>        }
>
>        private void textBox1_TextChanged(object sender, EventArgs e)
>        {
>
>        }
>
>        private void button1_Click(object sender, EventArgs e)
>        {
>            client = new IBClient();
>            client.ThrowExceptions = true;
>            client.Error += client_Error;
>            client.TickPrice += client_TickPrice;
>            client.TickSize += client_TickSize;
>            Console.WriteLine("Connecting to IB.");
>            client.Connect(textBox1.Text, Int32.Parse(textBox2.Text),
> 10);
>            //Forex Test
>            Forex EUR = new Forex("EUR", "USD");
>            client.RequestMarketData(17, EUR, null, false);
>
>        }
>        void client_TickSize(object sender, TickSizeEventArgs e)
>        {
>           appendToTextBox("Tick Size: " + e.Size + " Tick Type: " +
> EnumDescConverter.GetEnumDescription(e.TickType));
>        }
>
>         void client_Error(object sender, ErrorEventArgs e)
>        {
>            appendToTextBox("Error: " + e.ErrorMsg);
>        }
>
>         void client_TickPrice(object sender, TickPriceEventArgs e)
>        {
>            appendToTextBox("Price: " + e.Price + " Tick Type: " +
> EnumDescConverter.GetEnumDescription(e.TickType));
>        }
>
>
>    }
> }

Reply via email to