1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
   |  
Form1.cs
    public partial class Form1 : Form
    {
        private Test_Onchange DataSource;
        Thread Task1;
        private bool Flag_Stop_Task1;
        public Form1()
        {
            InitializeComponent();
            graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
            graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
            graph1.ChartAreas[0].AxisX.ScaleView.Size = 100;
 
            DataSource = new Test_Onchange();
            DataSource.ValueChanged += new EventHandler(EventValueChange);//Value input info
 
            Task1 = new Thread(new ThreadStart(Task_1));//create the thread
            Task1.Start();//start the thread
        }   
        protected virtual void EventValueChange(object sender, EventArgs e)
        {
            double valueMAX=0,size=0;
 
            if (graph1.InvokeRequired)
            {
                graph1.Invoke(new MethodInvoker(delegate { graph1.Series["ValueOnGraph"].Points.AddY(DataSource.Value); }));
                graph1.Invoke(new MethodInvoker(delegate { valueMAX = graph1.ChartAreas[0].AxisX.Maximum; }));
                graph1.Invoke(new MethodInvoker(delegate { size = graph1.ChartAreas[0].AxisX.ScaleView.Size; }));
                if (valueMAX - 10 > size)
                {
                    graph1.Invoke(new MethodInvoker(delegate { graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum); }));
                    graph1.Invoke(new MethodInvoker(delegate { graph1.Series["ValueOnGraph"].Points.RemoveAt(0); }));
                }
            }
        }
        private void Task_1()
        {
            while (!Flag_Stop_Task1)
            {
                Random RandVal = new Random();
                int Value = RandVal.Next(0, 100);
                DataSource.Value = Value;
                Thread.Sleep(100);
            }
            Flag_Stop_Task1 = false;
        }
 
        private void btn_StopTask_1_Click(object sender, EventArgs e)
        {
            Flag_Stop_Task1 = true;
        }  
} | 
Partager