首页 > 解决方案 > 在获取数据时使用 Winforms UI

问题描述

我使用 C# 开发表单应用程序。此应用程序将从光谱仪设备收集数据。当我设置连续采集时,在采集过程中我无法执行其他操作。我想在采集发生时缩放图表并执行其他操作。现在,当我单击缩放按钮时,它仅在采集结束后启用。请帮我解决这个问题。

请查看部分代码,其中包含单击以开始获取的按钮。一张图表也正在使用光谱仪的值进行更新。UI的屏幕截图也在底部。

 private void button2_Click(object sender, EventArgs e)
    {
        while (true)
        {
            this.Refresh();
            int numberOfPixels; // number of CCD elements
            double[] spectrum;
            spectrum = null;   // spectrometerIndex = 0;

            if (spectrometerIndex == -1)
                return; // no available spectrometer

            numberOfPixels = wrapper.getNumberOfPixels(spectrometerIndex);
            wrapper.setBoxcarWidth(spectrometerIndex, 0);
            wrapper.setCorrectForElectricalDark(spectrometerIndex, 1);
            wrapper.setIntegrationTime(spectrometerIndex, 1000); // acquisition time in microsecs

            int acquiretime = 100;
            if (textBoxin.Text != "")

            {
                int.TryParse(textBoxin.Text, out acquiretime); //arbitrary acquiretime

            }

            Stopwatch integrate = new Stopwatch();
            integrate.Start();
            while (integrate.Elapsed < TimeSpan.FromMilliseconds(acquiretime))
            {
                this.Refresh();
                spectrum = (double[])wrapper.getSpectrum(spectrometerIndex); data from spectrometer

            }

            integrate.Stop();
        }
    }

在此处输入图像描述

标签: c#winformsuser-interfacemultitasking

解决方案


推荐阅读