车辆信息统计报表管理系统--子、父线程调用的问题(委托)

信息无障碍 2021-07-24 13:25:45 阅读数:399

本文一共[544]字,预计阅读时长:1分钟~
随笔

 

        下面这个委托有问题,死循环会导致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;

 }

 

版权声明:本文为[信息无障碍]所创,转载请带上原文链接,感谢。 https://blog.51cto.com/u_14326985/3178664