?
Solved

Problem updating controls on UI main thread from back thread

Posted on 2005-03-05
4
Medium Priority
?
270 Views
Last Modified: 2010-04-16
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
Comment
Question by:breezback
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 13466898
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
 

Author Comment

by:breezback
ID: 13491324
Well , that's really good but I still have an exception I refresh a datagrid in DataArrived block.

Any ideas?

Thanks,

Ilan.
0
 

Author Comment

by:breezback
ID: 13494836
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question