求大神指导,小弟菜鸟一只,我最近写了段代码,通过动态链接库的类中的方法读取两组数据,每组的长度为2048,使用委托刷新窗体控件,结果界面非常卡,求大神指点代码:
#region 建立委托
public delegate void InvokeDelegate();
public delegate void SetTchartValue(AxTeeChart.AxTChart chart, double[] wavelength, double[] spectraData);
#endregion
#region
public InvokeDelegate SpectraDataInvokeDelegate;
public SetTchartValue setTchartValue;
#endregion
Thread _thread;
private void FormSpectraData_Load(object sender, EventArgs e)
{
GetTimeAndAvg();
toolTxtScanToAvg.Text = SpectraSuitInfo.spectraSuitInfoInstance.scanToAvg.ToString();
tooltxtIntergrationTime.Text = (SpectraSuitInfo.spectraSuitInfoInstance.intergrationTime/1000 ).ToString();
setTchartValue = new SetTchartValue(SetTchartValues);
SpectraDataInvokeDelegate = new InvokeDelegate(GetSpecstra);
}
private void toolBtnSure_Click(object sender, EventArgs e)
{
OpenSpectra();
_thread = new Thread(new ThreadStart(GetSpecstra));
SpectraSuitInfo.spectraSuitInfoInstance.isForm = true;
_thread.Start();
_thread.IsBackground = true;
}
SpectraSuitDAL spectraSuitDAL = new SpectraSuitDAL();
CCoWrapper Wrapper = new CCoWrapper();
private int intergration;
private int scantoavg;
#region 公开方法 得到光谱仪的积分时间和平均次数
public void GetTimeAndAvg()
{
SpectraSuitInfo.spectraSuitInfoInstance.intergrationTime = Convert.ToInt32(spectraSuitDAL.GetIntergrationTime(@"C:\Program Files\SpectraParams.ini", "SpectraParams", "积分时间"));
SpectraSuitInfo.spectraSuitInfoInstance.scanToAvg = Convert.ToInt32(spectraSuitDAL.GetScanToAVG(@"C:\Program Files\SpectraParams.ini", "SpectraParams", "平均次数"));
intergration = SpectraSuitInfo.spectraSuitInfoInstance.intergrationTime;
scantoavg = SpectraSuitInfo.spectraSuitInfoInstance.scanToAvg;
}
#endregion
#region 私有方法,设置光谱仪初始化及光谱仪连续触发
private bool SpectraSuit()
{
bool result = false;
Wrapper.CreateWrapper();
if (Wrapper.openAllSpectrometers() == 0)
{
result = false;
}
else
{
if (intergration != SpectraSuitInfo.spectraSuitInfoInstance.intergrationTime || scantoavg != SpectraSuitInfo.spectraSuitInfoInstance.scanToAvg)
{
Wrapper.closeAllSpectrometers();
intergration = SpectraSuitInfo.spectraSuitInfoInstance.intergrationTime;
scantoavg = SpectraSuitInfo.spectraSuitInfoInstance.scanToAvg;
spectraSuitDAL.SaveIntergrationTimeOrScanToAvg(@"C:\Program Files\SpectraParams.ini", "SpectraParams", "积分时间", Convert.ToString(intergration / 1000));
spectraSuitDAL.SaveIntergrationTimeOrScanToAvg(@"C:\Program Files\SpectraParams.ini", "SpectraParams", "平均次数", Convert.ToString(scantoavg));
Wrapper.openAllSpectrometers();
}
Wrapper.setCorrectForElectricalDark(0, 1);//去除暗电流
Wrapper.setIntegrationTime(0, intergration);
Wrapper.setScansToAverage(0, scantoavg);
Wrapper.setStrobeEnable(0, 1);
result = true;
}
return result;
}
private bool SetContinuousStobe()
{
bool result = false;
if (Wrapper.isFeatureSupportedContinuousStrobe(0))
{
CCoContinuousStrobe continuousStrobe = new CCoContinuousStrobe();
continuousStrobe.CreateContinuousStrobe();
continuousStrobe.setContinuousStrobeDelay(100000);
result = true;
}
else
{
result = false;
}
return result;
}
#endregion
#region 公共方法 组合光谱仪初始化和设置光谱仪连续触发
/// <summary>
/// 打开光谱仪
/// </summary>
public void OpenSpectra()
{
if (SpectraSuit())
{
if (SetContinuousStobe())
{ }
else
{
MessageBox.Show("该光谱仪不支持连续触发!请检查光谱仪的触发接线方式,非法操作,系统即将退出", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
}
else
{
MessageBox.Show("光谱仪不在线,原因可能是:USB接口松动或未连接光谱仪");
Application.Exit();
}
}
#endregion
#region 获得光谱仪的相关数据
public void GetSpecstra()
{
while (true)
{
SpectraSuitInfo.spectraSuitInfoInstance.spectraData = (double[])Wrapper.getSpectrum(0);
if(SpectraSuitInfo.spectraSuitInfoInstance.waveLength == null)
{
SpectraSuitInfo.spectraSuitInfoInstance.waveLength = (double[])Wrapper.getWavelengths(0);
}
if (SpectraSuitInfo.spectraSuitInfoInstance.isForm)
{
if (this.axTChart1.InvokeRequired)
{
this.axTChart1.BeginInvoke(setTchartValue, new object[] { axTChart1, SpectraSuitInfo.spectraSuitInfoInstance.waveLength, SpectraSuitInfo.spectraSuitInfoInstance.spectraData});
Thread.Sleep(500);
}
}
else
{
}
}
}
#endregion
#region 刷新窗体的TeeChart控件
public void SetTchartValues(AxTeeChart.AxTChart chart, double[] wavelength, double[] spectraData)
{
chart.Series(0).Clear();
for (int i = 0; i < wavelength.Length; i++)
{
chart.Series(0).AddXY(wavelength[i], spectraData[i], null, 0);
}
chart.Refresh();
}
#endregion
经过测试是这段代码的问题
SpectraSuitInfo.spectraSuitInfoInstance.spectraData = (double[])Wrapper.getSpectrum(0);
if (SpectraSuitInfo.spectraSuitInfoInstance.waveLength == null)
{
SpectraSuitInfo.spectraSuitInfoInstance.waveLength = (double[]) Wrapper.getWavelengths(0);
}
Wrapper是外部的动态链接库其中一个类的实例化。不是自己写的,用的别人的。没有源代码。
注意代码排版,这样看着很晕啊~
能留下个您的邮箱吗?我可以把源代码给您发过去
尽量写成后台进程,可THREAD 和 backgroudworker都行 你可以看看
已经写到后台了
_thread = new Thread(new ThreadStart(GetSpecstra));
SpectraSuitInfo.spectraSuitInfoInstance.isForm = true;
_thread.Start();
_thread.IsBackground = true;
请问您看这样写对吗?
GetSpecstra与 ui赋值 在后台进程中调用就可以了
@大笨鹰: 是的 如果还慢的话就在backgroundwork中调用这个线程 UI上基本感觉不到计算的延时