下面这个委托有问题,死循环会导致CPU 100%。子线程不能调用父线程。
while (!this.WinControl.IsHandleCreated)
{
}
this.WinControl.Invoke(dalegateBinding);//调用委托去刷新窗体
修改后的:
#region使用线程绑定数据
Thread ThreadBinding = null;
ThreadStart BindingStart = null;
delegate void BindingHander();
BindingHander dalegateBinding = null;
///<summary>
///线程加载数据
///</summary>
private void BindingData()
{
BindingStart = new ThreadStart(GetBindingData);
ThreadBinding = new Thread(BindingStart);
ThreadBinding.IsBackground = true;
ThreadBinding.Name = "EngineRotateSpeed_Analysis_Widget";
ThreadBinding.Start();
dalegateBinding = new BindingHander(DoBindingData);
}
DataSet ds = new DataSet();
WidgetDrive mWidgetDrive = new WidgetDrive();
///<summary>
///获取数据与绑定
///</summary>
void GetBindingData()
{
try
{
ds = DataLayerTest.Get_Engine_RotateSpeed_Analysis_List_Rpt(mWidgetDrive);//获取数据
if (this.WinControl==null||this.WinControl.IsDisposed)
{
}
else
{
while (!Helper.CanUse(this.WinControl))
{
}
this.WinControl.Invoke(dalegateBinding);//调用委托去刷新窗体
}
}
finally
{
ThreadBinding.Abort();
}
}
///<summary>
///用获取的DataSet分运行时间端绑定器件数据
///</summary>
void DoBindingData()
{
ZedGraphControl zgc = base.WinControl as ZedGraphControl;
switch (mWidgetDrive.ChartType)
{
case TimeType.Day:
CreateChartDay(ref zgc);//生成日图表
break;
case TimeType.Week:
CreateChartWeek(ref zgc);//生成周图表。
break;
case TimeType.Month:
CreateChartMonth(ref zgc);//生成月图表。
break;
}
zgc.Invalidate();
}
#endregion
// Helper类里面的CanUsr方法
internal static bool CanUse(Control control)
{
return control!=null&&!control.IsDisposed && control.IsHandleCreated;
}