我使用C#开发表单应用程序,以便从分光计设备收集数据。当我设置连续采集时,在采集发生的过程中,我不能使用UI执行其他操作。我正在考虑使用多线程。我来自科学背景,对C#不是很熟悉。也请帮助我一些代码。
请看部分代码,其中有一个按钮单击开始采集,另一个按钮保存采集的数据。我想保存数据,在两次采集之间发生。
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 !!");
}发布于 2021-02-16 17:29:59
如果您不熟悉多线程,您可以从使用BackgroundWorker开始,因为它提供了事件来轻松地更新UI。
因此,您的按钮将启动worker,并且worker将定期调用ReportProgress来更新UI
我最喜欢的与线程化http://www.albahari.com/threading/相关的资源
https://stackoverflow.com/questions/66221667
复制相似问题