简体   繁体   English

C#每隔几秒钟刷新一次图表

[英]C# refreshing a chart every few seconds

I have a Windows Forms App written in C#. 我有一个用C#编写的Windows窗体应用程序。 The idea is, that it draws a chart for 10 numbers after clicking a button. 想法是,单击按钮后,它将绘制10个数字的图表。 This works fine. 这很好。 I click the button, and I get a nice chart. 单击按钮,我得到一个漂亮的图表。 However I also want to include a sort of "auto refresh" mode, where the chart is refreshed every few seconds. 但是,我还想包括一种“自动刷新”模式,该模式每隔几秒钟刷新一次。 This would be enabled via Checkbox . 这可以通过Checkbox启用。 Here's my code: 这是我的代码:

private void chartButton_Click(object sender, EventArgs e) //draw a chart after the button is clicked
{
   Random rdn1 = new Random();
   int value;

   foreach (var series in ekran.Series) //clear previous values
   {
      series.Points.Clear();
   }

   for (int i = 0; i < 10; i++) //draw a chart from ten new values
   {
      value = rdn1.Next(0, 10); //for testing purpouses the value will be a random number a random number
      ekran.Series["seria1"].Points.AddXY(i, value);
   }
}

private void checkBox1_CheckedChanged(object sender, EventArgs e) 
{
   while(checkBox1.Checked) //click the chartButton every one second, when the checkbox is checked
   {
      //rysuj.PerformClick(); 
      chartButton.PerformClick();
      Thread.Sleep(1000);
   }
}   

And now for my problem. 现在是我的问题。 When I check the Checkbox , I will not get a chart until it finishes every iteration of the while loop. 当我选中Checkbox ,直到while循环的每次迭代完成,我都不会得到图表。 Since it's an infinite loop, I will never get my chart. 由于这是一个无限循环,因此我将永远无法获得图表。 If I rewrite the code to make only five iterations when the Checkbox is checked, I only get the chart for the fifth one (and after five seconds, as to be expected). 如果在选中“ Checkbox时重写代码以仅进行五次迭代,那么我只会得到第五张图表(在预期的五秒钟后)。

So my question is: how can I force this to draw a chart every time the button is clicked via chartButton.PerformClick() ? 所以我的问题是:每次通过chartButton.PerformClick()单击按钮时,如何强制它绘制图表? When I click the button manually, everything works fine, it's just when I try to do it automatically, I get my problem. 当我手动单击按钮时,一切正常,只是当我尝试自动进行操作时,我遇到了问题。

EDIT First of all,thank you for the replies. 编辑首先,感谢您的答复。 However, I'm still experiencing the same problem when using a timer. 但是,使用计时器时,我仍然遇到相同的问题。 This is how my code looks now: 这是我的代码现在的样子:

namespace ChartTest
{
public partial class Form1 : Form
{

    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();


    public Form1()
    {
        InitializeComponent();
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = 1000;
    }

    void timer_Tick(object sender, EventArgs e)
    {
        timer.Enabled = false;
        chartButton.PerformClick();

    }        

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {

        while (checkBox1.Checked)            
        {
            timer.Enabled = true;                       // Enable the timer
            timer.Start();                              // Start the timer
        }

    }

    private void chartButton_Click(object sender, EventArgs e)      //draw a chart after the button is clicked
    {
        Random rdn1 = new Random();
        int value;

        ekran.Series.Clear();

        var series2 = new System.Windows.Forms.DataVisualization.Charting.Series
        {
            Name = "Series2",
            Color = System.Drawing.Color.Green,
            IsVisibleInLegend = false,
            IsXValueIndexed = true,
            ChartType = SeriesChartType.Line
        };
        this.ekran.Series.Add(series2);

        for (int i = 0; i < 100; i++)
        {
            value = rdn1.Next(0, 10);
            series2.Points.AddXY(i, value);
        }

    }

}
}

Sorry for being a total noob, but I have no idea, what am I doing wrong this time. 很抱歉成为一个菜鸟,但我不知道,这次我在做什么错。

This is exactly what a Timer is for. 这正是Timer的用途。 Have the checkbox start/stop or enable/disable the timer, and handle the Timer.Tick event to redraw your chart. 使复选框启动/停止或启用/禁用计时器,并处理Timer.Tick事件以重绘图表。 In your case, the event handler could simply call chartButton.PerformClick() , or insert whatever code the PerformClick() does. 在您的情况下,事件处理程序可以简单地调用chartButton.PerformClick() ,或插入PerformClick()执行的任何代码。

ETA: If the chart refresh is not instant, you will probably want to push it off to a separate thread. 预计到达时间:如果图表刷新不是即时的,则可能需要将其推到单独的线程中。 If it's instant, there's not really any need to deal with the threading though. 如果是即时的,那么实际上并不需要处理线程。

I would go the route of using a thread with combination of checkbox's checkChange() event. 我会选择将线程与复选框的checkChange()事件结合使用。 Essentially this will allow your application to keep running while the update code will execute periodically. 本质上,这将使您的应用程序保持运行,同时更新代码将定期执行。 The refresh is determined by the sleep time, not your manual click or any other value.. Example below on how I to do this: 刷新取决于睡眠时间,而不是您的手动单击或其他任何值。.下面有关如何执行此操作的示例:

Thread refreshThread = null;

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
   if (refreshThread == null) //No thread running, assume it starts this way
   {
      refreshThread = new Thread(chartRefresh);
      refreshThread.Start();
   }
   else //Thread is running, must terminate
   {
      refreshThread.Abort();
      refreshThread = null;
   }
}

private void chartRefresh()
{
   while (true)
   {
      //code to refresh chart
      Thread.Sleep(10000);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM