首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Windows窗体中分离UI线程和进程线程

如何在Windows窗体中分离UI线程和进程线程
EN

Stack Overflow用户
提问于 2021-02-16 17:22:06
回答 1查看 66关注 0票数 0

我使用C#开发表单应用程序,以便从分光计设备收集数据。当我设置连续采集时,在采集发生的过程中,我不能使用UI执行其他操作。我正在考虑使用多线程。我来自科学背景,对C#不是很熟悉。也请帮助我一些代码。

请看部分代码,其中有一个按钮单击开始采集,另一个按钮保存采集的数据。我想保存数据,在两次采集之间发生。

代码语言:javascript
复制
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();
        }
    }

private void button7_Click(object sender, EventArgs e)
        {
            File.WriteAllText((@"D:\ExecRonefile\abcd.csv"), csv.ToString());
            MessageBox.Show("Data saved into csv format succesfully !!");
            
        }
EN

回答 1

Stack Overflow用户

发布于 2021-02-16 17:29:59

如果您不熟悉多线程,您可以从使用BackgroundWorker开始,因为它提供了事件来轻松地更新UI。

因此,您的按钮将启动worker,并且worker将定期调用ReportProgress来更新UI

我最喜欢的与线程化http://www.albahari.com/threading/相关的资源

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66221667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档