• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Problem updating controls on UI main thread from back thread

Hey,

I'm have a trader object , who manages transactions.
I want to update a UI in real-time without changing my trader's logic.
So , I added some events that the trader could raise , OnStartTransaction , OnReadNewData etc ..
I thought that I could start a thread in the back to run the trader.
And catch its events on main UI thread , in which I would update the controls.

Here is my code:

partial class SmartTraderGUI : Form
    {
        delegate void RunTraderDelegate();
        RunTraderDelegate traderDel;
        IAsyncResult ar;
        Trader trader;

        public SmartTraderGUI()
        {
            InitializeComponent();
        }

        private void SmartTraderGUI_Load(object sender, EventArgs e)
        {
            trader = new Trader(@"d:\Perso\SmartTrader\SmartTrader\data.txt");
            trader.OnTransactionStarted += new Trader.TraderHandler(StartedTransaction);
            trader.OnTransactionClosed += new Trader.TraderHandler(ClosedTransaction);
            trader.OnReadNewData += new Trader.DataHandler(NewDataArrived);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            traderDel = new RunTraderDelegate(RunTrader);
            ar = traderDel.BeginInvoke(null,null);
        }

        public void RunTrader()
        {
            while (trader.ReadNewData())
            {
                int id = trader.StartTransaction();
                if (id > 0)
                {
                    while (trader.ContinueTransaction())
                    {
                        //wait to close
                    }
                    trader.CloseTransaction();
                }
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
       
        }

        private void StartedTransaction(object sender, TransacArgs args)
        {
            txtRate.Text = args.CurrentTransaction.Rate.ToString();
            lblStatusColor.BackColor = Color.Green;
        }

        private void ClosedTransaction(object sender, TransacArgs args)
        {
            txtClosingRate.Text = args.CurrentTransaction.ClosingRate.ToString();
            lblStatusColor.BackColor = Color.Red;
        }

        private void NewDataArrived(object sender, DataArgs args)
        {
            txtAsk.Text = args.Ask;
            txtBid.Text = args.Bid;
            lblLiveDate.Text = args.Date.ToString();
        }
}

When I run it I get the error : Illegal cross-thread operation: Control 'txtAsk' accessed from a thread other than the thread it was created on.

Thanks in advance,

Very urgent, :)
0
breezback
Asked:
breezback
  • 3
1 Solution
 
AlexFMCommented:
Form controls may be accessed only from the thread which created this form. To call form methods from a worker thread, use BeginInvoke or Invoke methods.
http://www.codeproject.com/csharp/workerthread.asp

    public delegate void DelegateDataArrived(DataArgs args);   // declare this before class

        private void NewDataArrived(object sender, DataArgs args)
        {
               this.BeginInvoke(new DelegateDataArrived(this.DataArrived), new Object[] {args});
        }

        // Update form in the main thread
        private void DataArrived(DataArgs args)
        {
            txtAsk.Text = args.Ask;
            txtBid.Text = args.Bid;
            lblLiveDate.Text = args.Date.ToString();
        }
0
 
breezbackAuthor Commented:
Well , that's really good but I still have an exception I refresh a datagrid in DataArrived block.

Any ideas?

Thanks,

Ilan.
0
 
breezbackAuthor Commented:
0
 
breezbackAuthor Commented:
My most biggest problem here is not the exception (cause with no breakpoints it's fine),
But the RefreshHistoryData that doesn't update the grid on the GUI.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now