我使用 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 !!");
}