如何加速具有多个后台工作者的应用程序

时间:2014-09-17 13:50:26

标签: c# .net winforms checkbox backgroundworker

我有76个复选框。每次选中一个复选框时,我的应用程序都会创建一个后台工作程序并在其中执行一些操作。其中一项任务是将从数据库中提取的值写入特定复选框旁边的标签。但是,当我检查复选框时,非常快的标签会等待一段时间,然后显示该值。 (我添加到图表中的点也等了)像这样;

enter image description here

如果您看到某些标签虽然选中了复选框,但不会显示任何值。它们在几秒钟后显示出它们的价值。

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked == true)
            {
                int index = pnlAdres.Controls.IndexOf(((CheckBox)sender));
                if (workers[index] == null)
                {
                    workers[index] = new BackgroundWorker();
                    workers[index].DoWork += new DoWorkEventHandler(worker_DoWork);
                    workers[index].RunWorkerAsync(sender);
                }
                else
                {
                    if (!workers[index].IsBusy)
                    {
                        workers[index].RunWorkerAsync(sender);
                    }
                }
            }

        }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        AddSeries(((CheckBox)e.Argument).Text);
        SpecifyChartType(((CheckBox)e.Argument).Text, SeriesChartType.Line);
        SpecifyXValueType(((CheckBox)e.Argument).Text, ChartValueType.Time);

        ClearPoints(((CheckBox)e.Argument).Text);

        //chartMonitor.Series[((CheckBox)e.Argument).Text].Points.AddXY(DateTime.Now.ToLongTimeString(), 0);

        SetChartArea(((CheckBox)e.Argument).Text, chartMonitor.ChartAreas[0].Name);
        SetSerieBorderWidth(((CheckBox)e.Argument).Text, 2);

        if (((CheckBox)e.Argument).ForeColor != Color.White)
            SetSerieColor(((CheckBox)e.Argument).Text, ((CheckBox)e.Argument).ForeColor);

        int adresID = Convert.ToInt32(((CheckBox)e.Argument).Tag);
        DateTime startDate = dtpMonitor.Value;
        DateTime finishDate = dtpFinish.Value;
        using (var db = new GEOTEKVESTELEntities())
        {
            var vv = (from deger in db.tblDeger join adres in db.tblAdres on deger.AdresID equals adres.ID where deger.AdresID == adresID && deger.DegerDonguTarih >= startDate && deger.DegerDonguTarih <= finishDate orderby deger.DegerDonguTarih select new { deger, adres }).ToList();
            for (int i = 0; i < vv.Count; i++)
            {
                this.chartMonitor.Series[((CheckBox)e.Argument).Text].Points.AddXY(((DateTime)vv[i].deger.DegerDonguTarih).ToLongTimeString(), vv[i].deger.Deger);
            }
        }
        int tag = Convert.ToInt32(((CheckBox)e.Argument).Tag);

            using (var db = new GEOTEKVESTELEntities())
            {
                while (((CheckBox)e.Argument).Checked)
                {
                    var v = (from deger in db.tblDegerSon join adres in db.tblAdres on deger.AdresID equals adres.ID where deger.AdresID == tag select new { deger, adres }).ToList();
                    int l = pnlAdres.Controls.IndexOf(((CheckBox)e.Argument));

                    DateTime now = DateTime.Now;
                    if (IsDisposed)
                    {
                        return;
                    }
                    if (pnlValue.Controls[l].Text != v[0].deger.Deger + " " + units[l])
                        SetText(pnlValue, v[0].deger.Deger.ToString(), l, true);
                    if (IsDisposed)
                    {
                        return;
                    }
                    AddXYPoints(v[0].adres.AdresOPC, now, v[0].deger.Deger);


                    System.Threading.Thread.Sleep(1000);
                }
            }

        ClearPoints(((CheckBox)e.Argument).Text);
        RemoveSerie(((CheckBox)e.Argument).Text);
        int index = pnlAdres.Controls.IndexOf(((CheckBox)e.Argument));
        SetText(pnlValue, "", index, false);
        SetText(pnlM1, "", index, false);
        SetText(pnlM2, "", index, false);
    }

    private void RemoveSerie(string name)
    {
        if (chart.InvokeRequired)
        {
            RemoveSerieCallback d = new RemoveSerieCallback(RemoveSerie);
            this.Invoke(d, new object[] { name});
        }
        else
        {
            chart.Series.Remove(chartMonitor.Series[name]);
        }
    }

    private void SetSerieColor(string name, Color color)
    {
        if (chart.InvokeRequired)
        {
            SetSerieColorCallback d = new SetSerieColorCallback(SetSerieColor);
            this.Invoke(d, new object[] { name, color });
        }
        else
        {
            chart.Series[name].Color = color; ;
        }
    }

    private void SetSerieBorderWidth(string name, int width)
    {
        if (chart.InvokeRequired)
        {
            SetSerieBorderWidthCallback d = new SetSerieBorderWidthCallback(SetSerieBorderWidth);
            this.Invoke(d, new object[] { name, width });
        }
        else
        {
            chart.Series[name].BorderWidth = width ;
        }
    }

    private void SetChartArea(string name, string chartname)
    {
        if (chart.InvokeRequired)
        {
            SetChartAreaCallback d = new SetChartAreaCallback(SetChartArea);
            this.Invoke(d, new object[] { name, chartname });
        }
        else
        {
            chart.Series[name].ChartArea = chartname;
        }
    }

    private void ClearPoints(string name)
    {
        if (chart.InvokeRequired)
        {
            ClearPointsCallback d = new ClearPointsCallback(ClearPoints);
            this.Invoke(d, new object[] {name});
        }
        else
        {
            chart.Series[name].Points.Clear();
        }
    }

    private void SpecifyChartType(string name, SeriesChartType type)
    {
        if (chart.InvokeRequired)
        {
            SpecifyChartTypeCallback d = new SpecifyChartTypeCallback(SpecifyChartType);
            this.Invoke(d, new object[] { name, type });
        }
        else
        {
            chart.Series[name].ChartType = type;
        }
    }

    private void SpecifyXValueType(string name, ChartValueType type)
    {
        if (chart.InvokeRequired)
        {
            SpecifyXValueTypeCallback d = new SpecifyXValueTypeCallback(SpecifyXValueType);
            this.Invoke(d, new object[] { name, type });
        }
        else
        {
            chart.Series[name].XValueType = type;
        }
    }

    private void AddSeries(string name)
    {
        if (chart.InvokeRequired)
        {
            AddSeriesCallback d = new AddSeriesCallback(AddSeries);
            this.Invoke(d, new object[] { name});
        }
        else
        {
            chart.Series.Add(name);
        }
    }

如何加速我的申请。我希望这些标签快速显示其价值。我想快速为我的图表添加点数

0 个答案:

没有答案