2015-07-02 88 views
0

我有绘制图表一个的OnPaint函数:Winform的重画在5秒

protected override void OnPaint(PaintEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     cs.ChartArea = this.ClientRectangle; 
     AddData(); 
     SetPlotArea(g); 
     cs.AddChartStyle(g); 
     dc.AddLines(g, cs); 
     //lg.AddLegend(g, dc, cs); 
     g.Dispose(); 
    } 

我添加了一个计时器内部Form1中()应该在每5秒重绘图表:

var timer = new Timer(); 
timer.Tick += new EventHandler(timer_Tick); 
timer.Interval = 5000; 
timer.Start(); 

和time_Tick函数:

void timer_Tick(object sender, EventArgs e) 
    { 
     //Repaint the chart 
    } 

可以使用timer_Tick函数每5秒重绘一次图表吗?

+0

放'this.Invalidate();'在蜱代码。目前还不清楚你在哪里创建定时器,但它看起来可能会泄漏内存 - 最好是在窗体范围内声明'private timer = new Timer();'。 – LarsTech

+1

不要处理该图形对象,也不要创建它。 – LarsTech

+0

我在Form1()方法中添加了计时器。如果你知道一个更好的地方,请让我知道。 – Dana

回答

1

试试这个:

void timer_Tick(object sender, EventArgs e) 
{ 
    this.invalidate(); 
}