views:

102

answers:

2

Hi 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

A: 

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:

// The declaration of the textbox.
private TextBox m_TextBox;
public delegate void UpdateTextCallback(string text);

Then you'd have a method that performs the actual change

// Updates the textbox text.
private void UpdateText(string text)
{
  // Set the textbox text.
  m_TextBox.Text = text;
}

And then in your separate thread, call this via

m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText),  new object[]{”Text generated on non-UI thread.”});
Stephen Wrighton
it should be in same time , forex data is chaging so fast i need to do it as the timer , the question i wanna ask is , how i can , First time Add controls ,and then on each tick , just change the Text of buttons in my custom control , it will heavily optimize the code , is there any solution available ? that i can access cutom control that is added at runtime , and change the properties .
^making this outside the timer class is stil a prblem , coz i dont know the numbers of rows returning from resposnse from api , it could be 2-20 , if somehow i itialize them outside the timer event , i m still not able to access the control like this_userControl.btnlay1.Text = xxx
+1  A: 

In order to access the control multiple times, you need to make the scope of the variable larger than just the tick method. See the example of making it a class variable. You could also put the control constructor in the form constructor and then the tick method would only work to change the data.

public MyForm : Form
{
    private UserControl _userControl = null;
    ...
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_userControl == null)
            //make control
        //set control data
    }
}
unholysampler
making this outside the timer class is stil a prblem , coz i dont know the numbers of rows returning from resposnse from api , it could be 2-20 , if somehow i itialize them outside the timer event , i m still not able to access the control like this _userControl.btnlay1.Text