帮助更新图表

发布于 2024-07-29 02:36:46 字数 8720 浏览 8 评论 0原文

我有两种协同工作的方法,它们生成一个大的双数组(每个 1/1000000S 5000 项),并且该数组应该显示一个图表(Dundas 图表)。 但图表没有更新。

请帮我!

对不起,我的英语不好!

这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Dundas.Charting.WinControl;
using Dundas.Charting.WinControl.Utilities;


namespace Online_Detector
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region Fields
        double fName = 0;
        public object dataDigitalGlobal;
        long num;
        #endregion

        #region Method
        private void ManualSavetoFile(double Value, double Time)
        {
            //Get Path
            //fName += 1;
            string IntLocation = Convert.ToString(fName) + ".txt";

            #region Write File

            FileStream FOut = null;

            try
            {

                if (File.Exists(IntLocation))
                    FOut = new FileStream(IntLocation, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 4096, true);
                else
                    FOut = new FileStream(IntLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 4096, true);

                StreamWriter SOut = new StreamWriter(FOut);


                // <Add Line To Export File >
                lock (this)
                {
                    if (FOut.CanWrite)
                    {
                        SOut.WriteLine(Value);
                        SOut.WriteLine(Time);
                    }
                }
                // <Add Line To Export File >

                SOut.Close();
                FOut.Close();

            }

            catch (IOException Err)
            {
                MessageBoxManager.OK = "&تاييد";
                MessageBoxManager.Register();
                MessageBox.Show("خطاي زير در عمليات تهيه فايل خروجي رخ داد:" + "\r" + Err.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                MessageBoxManager.Unregister();
            }

            catch (FieldAccessException Err)
            {
                MessageBoxManager.OK = "&تاييد";
                MessageBoxManager.Register();
                MessageBox.Show("خطاي زير در عمليات تهيه فايل خروجي رخ داد:" + "\r" + Err.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                MessageBoxManager.Unregister();
            }

            catch (Exception Err)
            {
                MessageBoxManager.OK = "&تاييد";
                MessageBoxManager.Register();
                MessageBox.Show("خطاي زير در عمليات تهيه فايل خروجي رخ داد:" + "\r" + Err.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                MessageBoxManager.Unregister();
            }

            finally
            {
                FOut.Close();
            }

            #endregion //Write File

        }
        #endregion

        #region Event on Tab Report
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            { 
                //check all the setting
                axAdvAICtrl.ChannelScanStart = Convert.ToInt32(txtChannelStart.Text);
                axAdvAICtrl.ChannelScanCount = Convert.ToInt32(txtChannelCount.Text);

                int count = int.Parse(txtDataCount.Text);

                // Ocx allocate the buffer of DataDigital ,equal to new object!             
                dataDigitalGlobal = null;
                // Engage the FAI with Asychronous mode
                count = axAdvAICtrl.AcquireBulkDataToMemory(count, out dataDigitalGlobal, -1, chkCylic.Checked, false);
                num = 0;
                // Disable all controls on the form
                btnStart.Enabled = false;
                // and only Enable the Stop button
                btnEnd.Enabled = true;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error");
                axAdvAICtrl.StopAcquireBulkData(0);
            }

        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            //stop the acquisition
            axAdvAICtrl.StopAcquireBulkData(0);

            // Enable all controls on the form
            btnStart.Enabled = true;
            // and only Disable the Stop button
            btnEnd.Enabled = false;
        }

        #endregion 
        //this event every 1/500000
        private void axAdvAICtrl_OnFirstHalfBulkDataReady(object sender, AxAdvAILib._IAdvAIEvents_OnFirstHalfBulkDataReadyEvent e)
        {
            #region "Get Data "
            try
            {
                object analogArray = e.analogArray;
                long i;

                if (analogArray != null)
                {
                    float[] voltage;
                    voltage = (float[])analogArray;
                    for (i = 0; i < Convert.ToInt32(txtDataCount.Text) / 2; i++)
                    {
                        // Write Data To Manual file
                        ManualSavetoFile(voltage[i], (num / axAdvAICtrl.DataSampleRate));

                        // Define some variables
                        int numberOfPointsInChart = Convert.ToInt32(txtDataCount.Text);
                        int numberOfPointsAfterRemoval = 1;

                        chaIon.Series["Series1"].Points.AddXY(xValue[arrayCounter], yValue[arrayCounter]);

                        // Keep a constant number of points by removing them from the left
                        while (chaIon.Series[0].Points.Count > numberOfPointsInChart)
                        {
                            // Remove data points on the left side
                            while (chaIon.Series[0].Points.Count > numberOfPointsAfterRemoval)
                            {
                                chaIon.Series[0].Points.RemoveAt(0);
                            }

                        }
                        chaIon.Invalidate();
                        num++;
                    }
                }
            }
            catch (Exception err)
            {
                threadChart.Abort();
                MessageBox.Show(err.Message, "Error");
            }
            #endregion
        }

        //this event every 1/500000
        private void axAdvAICtrl_OnSecondHalfBulkDataReady(object sender, AxAdvAILib._IAdvAIEvents_OnSecondHalfBulkDataReadyEvent e)
        {
            #region "Get Data "
            try
            {
                object analogArray = e.analogArray;
                long i;

                if (analogArray != null)
                {
                    float[] voltage;
                    voltage = (float[])analogArray;
                    for (i = 0; i < Convert.ToInt32(txtDataCount.Text) / 2; i++)
                    {
                        // Write Data To Manual file
                        ManualSavetoFile(voltage[i], (num / axAdvAICtrl.DataSampleRate));

                        // Define some variables
                        int numberOfPointsInChart = Convert.ToInt32(txtDataCount.Text);
                        int numberOfPointsAfterRemoval = 1;

                        chaIon.Series["Series1"].Points.AddXY(xValue[arrayCounter], yValue[arrayCounter]);

                        // Keep a constant number of points by removing them from the left
                        while (chaIon.Series[0].Points.Count > numberOfPointsInChart)
                        {
                            // Remove data points on the left side
                            while (chaIon.Series[0].Points.Count > numberOfPointsAfterRemoval)
                            {
                                chaIon.Series[0].Points.RemoveAt(0);
                            }

                        }
                        chaIon.Invalidate();
                        num++;
                    }
                }
            }
            catch (Exception err)
            {
                threadChart.Abort();
                MessageBox.Show(err.Message, "Error");
            }
            #endregion
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            axAdvAICtrl.StopAcquireBulkData(0);
        }
    }
}

I have two methods that work together and they make a big double array (every 1/1000000S 5000 item) and this array should show a chart (Dundas chart).
But the chart is not updating.

Please help me!

Sorry for my bad English!

This my code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Dundas.Charting.WinControl;
using Dundas.Charting.WinControl.Utilities;


namespace Online_Detector
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region Fields
        double fName = 0;
        public object dataDigitalGlobal;
        long num;
        #endregion

        #region Method
        private void ManualSavetoFile(double Value, double Time)
        {
            //Get Path
            //fName += 1;
            string IntLocation = Convert.ToString(fName) + ".txt";

            #region Write File

            FileStream FOut = null;

            try
            {

                if (File.Exists(IntLocation))
                    FOut = new FileStream(IntLocation, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 4096, true);
                else
                    FOut = new FileStream(IntLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 4096, true);

                StreamWriter SOut = new StreamWriter(FOut);


                // <Add Line To Export File >
                lock (this)
                {
                    if (FOut.CanWrite)
                    {
                        SOut.WriteLine(Value);
                        SOut.WriteLine(Time);
                    }
                }
                // <Add Line To Export File >

                SOut.Close();
                FOut.Close();

            }

            catch (IOException Err)
            {
                MessageBoxManager.OK = "&تاييد";
                MessageBoxManager.Register();
                MessageBox.Show("خطاي زير در عمليات تهيه فايل خروجي رخ داد:" + "\r" + Err.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                MessageBoxManager.Unregister();
            }

            catch (FieldAccessException Err)
            {
                MessageBoxManager.OK = "&تاييد";
                MessageBoxManager.Register();
                MessageBox.Show("خطاي زير در عمليات تهيه فايل خروجي رخ داد:" + "\r" + Err.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                MessageBoxManager.Unregister();
            }

            catch (Exception Err)
            {
                MessageBoxManager.OK = "&تاييد";
                MessageBoxManager.Register();
                MessageBox.Show("خطاي زير در عمليات تهيه فايل خروجي رخ داد:" + "\r" + Err.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                MessageBoxManager.Unregister();
            }

            finally
            {
                FOut.Close();
            }

            #endregion //Write File

        }
        #endregion

        #region Event on Tab Report
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            { 
                //check all the setting
                axAdvAICtrl.ChannelScanStart = Convert.ToInt32(txtChannelStart.Text);
                axAdvAICtrl.ChannelScanCount = Convert.ToInt32(txtChannelCount.Text);

                int count = int.Parse(txtDataCount.Text);

                // Ocx allocate the buffer of DataDigital ,equal to new object!             
                dataDigitalGlobal = null;
                // Engage the FAI with Asychronous mode
                count = axAdvAICtrl.AcquireBulkDataToMemory(count, out dataDigitalGlobal, -1, chkCylic.Checked, false);
                num = 0;
                // Disable all controls on the form
                btnStart.Enabled = false;
                // and only Enable the Stop button
                btnEnd.Enabled = true;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error");
                axAdvAICtrl.StopAcquireBulkData(0);
            }

        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            //stop the acquisition
            axAdvAICtrl.StopAcquireBulkData(0);

            // Enable all controls on the form
            btnStart.Enabled = true;
            // and only Disable the Stop button
            btnEnd.Enabled = false;
        }

        #endregion 
        //this event every 1/500000
        private void axAdvAICtrl_OnFirstHalfBulkDataReady(object sender, AxAdvAILib._IAdvAIEvents_OnFirstHalfBulkDataReadyEvent e)
        {
            #region "Get Data "
            try
            {
                object analogArray = e.analogArray;
                long i;

                if (analogArray != null)
                {
                    float[] voltage;
                    voltage = (float[])analogArray;
                    for (i = 0; i < Convert.ToInt32(txtDataCount.Text) / 2; i++)
                    {
                        // Write Data To Manual file
                        ManualSavetoFile(voltage[i], (num / axAdvAICtrl.DataSampleRate));

                        // Define some variables
                        int numberOfPointsInChart = Convert.ToInt32(txtDataCount.Text);
                        int numberOfPointsAfterRemoval = 1;

                        chaIon.Series["Series1"].Points.AddXY(xValue[arrayCounter], yValue[arrayCounter]);

                        // Keep a constant number of points by removing them from the left
                        while (chaIon.Series[0].Points.Count > numberOfPointsInChart)
                        {
                            // Remove data points on the left side
                            while (chaIon.Series[0].Points.Count > numberOfPointsAfterRemoval)
                            {
                                chaIon.Series[0].Points.RemoveAt(0);
                            }

                        }
                        chaIon.Invalidate();
                        num++;
                    }
                }
            }
            catch (Exception err)
            {
                threadChart.Abort();
                MessageBox.Show(err.Message, "Error");
            }
            #endregion
        }

        //this event every 1/500000
        private void axAdvAICtrl_OnSecondHalfBulkDataReady(object sender, AxAdvAILib._IAdvAIEvents_OnSecondHalfBulkDataReadyEvent e)
        {
            #region "Get Data "
            try
            {
                object analogArray = e.analogArray;
                long i;

                if (analogArray != null)
                {
                    float[] voltage;
                    voltage = (float[])analogArray;
                    for (i = 0; i < Convert.ToInt32(txtDataCount.Text) / 2; i++)
                    {
                        // Write Data To Manual file
                        ManualSavetoFile(voltage[i], (num / axAdvAICtrl.DataSampleRate));

                        // Define some variables
                        int numberOfPointsInChart = Convert.ToInt32(txtDataCount.Text);
                        int numberOfPointsAfterRemoval = 1;

                        chaIon.Series["Series1"].Points.AddXY(xValue[arrayCounter], yValue[arrayCounter]);

                        // Keep a constant number of points by removing them from the left
                        while (chaIon.Series[0].Points.Count > numberOfPointsInChart)
                        {
                            // Remove data points on the left side
                            while (chaIon.Series[0].Points.Count > numberOfPointsAfterRemoval)
                            {
                                chaIon.Series[0].Points.RemoveAt(0);
                            }

                        }
                        chaIon.Invalidate();
                        num++;
                    }
                }
            }
            catch (Exception err)
            {
                threadChart.Abort();
                MessageBox.Show(err.Message, "Error");
            }
            #endregion
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            axAdvAICtrl.StopAcquireBulkData(0);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-08-05 02:36:46

通常,对 UI 组件的任何更新都应通过 Control.Invoke(或 WPF 等效项)在 UI 线程上完成。 您的更改可能会导致异常被某些地方吞噬。

你的代码在 UI 线程中工作吗? 您仍然可以使用 BackgroundWorker - 只需通过 Invoke 进行更新(最好是批量更新)。

Typically, any updates to UI components should be done on the UI thread, via Control.Invoke (or the WPF equivalent). It is possible that your changes are causing exceptions that are being swallowed somewhere.

Does your code work frmo the UI thread? You can still use a BackgroundWorker - you just need to do the updates (ideally in batches) via Invoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文