2010-05-29 181 views
2

餘米添加自定義的控制,我FlowLayoutPanel的,它的某種外匯的數據,每秒刷新一次,自定義控件屬性 - C#,窗體

等各個計時器滴答,即時通訊增加一個控制,改變控制按鈕上的文字,隨後將其加入爲FlowLayout面板,

IM在每100毫秒計時器滴答做,

它佔用過多的CPU,

這裏是我的自定義控制。

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(); 
    }  

這裏是我如何添加控制;

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(); 
    } 
} 

現在它在每次發送時都發送10次,佔我Core2Duo CPU的60%。 我想刷新快,我需要一些優化建議

是否有任何其他的方式,我可以只添加控件第一次,然後更改自定義的文本控件上的每個刷新或計時器滴答上運行按鈕

即時通訊使用c#.net

+0

請不要在SO上覆制/粘貼代碼而不要格式化。這太可怕了。請在SO上張貼之前檢查拼寫。謝謝 – abatishchev 2010-05-30 14:59:27

回答

0

它發生10次,因爲你告訴它。

在第二

有1000毫秒,如果你正在做的每100ms,那麼你在執行動作10次。


好的,這裏有一些事情正在進行。首先,您需要爲要更新的各種控件維護一個處理程序或名稱。這可以通過定義的對象或使用FINDCONTROL(「ControlName」)來完成。

現在,針對您在訪問控制器時遇到的問題,這是由於THREADS造成的! Timer_Elapsed事件中的代碼發生在一個單獨的線程中,因此,要影響UI,您需要使代碼線程安全。

考慮這些對象:

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

然後你必須執行的實際變化

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

,然後在單獨的線程,通過

m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText), new object[]{」Text generated on non-UI thread.」}); 
+0

它應該是在同一時間,外匯數據被換款如此之快,我需要做的計時器, 我想這個問題問的是, 我怎麼能,第一次添加控件, 上 然後每次打勾,只需更改我的自定義控件中的按鈕文本,就會大量優化代碼, 有沒有可用的解決方案?我可以訪問在運行時添加的cutom控件,並更改屬性。 – kawafan 2010-05-29 13:48:13

+0

^ 使這個定時器類外是一個還是老樣子prblem,怎麼我不知道從行從resposnse返回API的數量, 也可能是2-20, 如果不知何故,我itialize他們定時器事件外, 即時通訊仍然無法訪問這樣的控制 _userControl.btnlay1.Text = xxx – kawafan 2010-05-29 14:10:00

1
調用此方法

爲了多次訪問控件,您需要使變量的作用域大於勾號方法。查看使其成爲類變量的示例。您也可以將控件構造函數放在窗體構造函數中,然後tick方法只會改變數據。

public MyForm : Form 
{ 
    private UserControl _userControl = null; 
    ... 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (_userControl == null) 
      //make control 
     //set control data 
    } 
} 
+0

使這個以外的計時器類是stil一個問題,因爲我不知道從api返回從resposnse返回的行數,它可能是2 -20,如果我以某種方式將它們在計時器事件之外重新啓用,im仍然無法像這樣訪問控件_userControl.btnlay1.Text – kawafan 2010-05-29 21:32:01