I m adding custom control to my flowlayoutpanel , its a sort of forex data , refresh every second ,
so on each timer tick , i m adding a control , changing controls button text , then adding it to flowlayout panel ,
i m doing it at each 100ms timer tick ,
it taking too much CPU ,
here is my custom Control .
public partial class UserControl1 : UserControl
{
public void displaydata(string name, string back3price, string back3, string back2price, string back2, string back1price, string back1, string lay3price, string lay3 , string lay2price, string lay2, string lay1price, string lay1)
{
lblrunnerName.Text = name.ToString();
btnback3.Text = back3.ToString() + "\n" + back3price.ToString();
btnback2.Text = back2.ToString() + "\n" + back2price.ToString();
btnback1.Text = back1.ToString() + "\n" + back1price.ToString();
btnlay1.Text = lay1.ToString() + "\n" + lay1price.ToString();
btnlay2.Text = lay2.ToString() + "\n" + lay2price.ToString();
btnlay3.Text = lay3.ToString() + "\n" + lay3price.ToString();
}
and here is how i m adding control;
private void timer1_Tick(object sender, EventArgs e)
{
localhost.marketData[] md;
md = ser.getM1();
flowLayoutPanel1.Controls.Clear();
foreach (localhost.marketData item in md)
{
UserControl1 ur = new UserControl1();
ur.Name = item.runnerName + item.runnerID;
ur.displaydata(item.runnerName, item.back3price, item.back3, item.back2price, item.back2, item.back1price, item.back1, item.lay3price, item.lay3, item.lay2price, item.lay2, item.lay1price, item.lay1);
flowLayoutPanel1.SuspendLayout();
flowLayoutPanel1.Controls.Add(ur);
flowLayoutPanel1.ResumeLayout();
}
}
now its happing on 10 times on each send , taking 60% of my Core2Duo CPU .
i want to refresh it fast , i need some optimization tips
is there any other way , i can just add controls first time , and then change the text of custom controls buttons on runtime on each refresh or timer tick
i m using c# .Net
it’s happening 10 times a second because you’re telling it to.
there’s 1000ms in a second
if you’re doing it every 100ms, then you’re performing the action 10 times a second.
Okay, there’s a few things going on here. First, you’ll need to maintain a handler or a NAME for the various controls that are to be updated. This can be done either via an defined object, or using the FINDCONTROL(“ControlName”).
Now, for your issue with the accessing the control in the timer control, that’s due to THREADS! The code inside the Timer_Elapsed event happens on a sepeaate thread, thus, to impact the UI you need to make your code thread safe.
Consider these objects:
Then you’d have a method that performs the actual change
And then in your separate thread, call this via