@Wing0

ProgressBar в Parallel.Invoke?

Здравствуйте, поможет кто-нибудь сделать нормальный progressbar в программе? Если откомментировать Value, то progressbar дойдет до конца, но перед этим он сбрасывается один раз, из-за чего вычисления идут в 2 раза дольше.
private void выполнитьРасчетToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        var before = DateTime.Now;
        label5.Text = "Идет расчет...";
        //this.Lockk();
        progressBar1.Visible = true;
        double b;
        double c;
        double q;
        double w;
        double d = 0;//theta1
        double dd = 0;
        double m = 0;//phi1
        double mm = 0;
        double f = 0;
        double ff = 0;
        double g = 0;//theta2
        double gg = 0;
        double h = 0;//phi2
        double hh = 0;
        double i1 = 0;
        double ii1 = 0;
        double k = 0;
        double kk = 0;
        int l = 0;
        int ll = 0;
        if (checkBox1.Checked && checkBox2.Checked) progressBar1.Maximum = Prec + Prec1;
        if (checkBox1.Checked && !checkBox2.Checked) progressBar1.Maximum = Prec;
        if (!checkBox1.Checked && checkBox2.Checked) progressBar1.Maximum = Prec1;
        var ob1 = new object();
        var ob2 = new object();
        progressBar1.Value = 0;
        ListCo = new List<double[]>();
        ListCounter = new List<double[]>();

        //if (checkBox1.Checked)
        //{
        //    for (int i = 0; i < Prec; i++)
        //    {
        //        progressBar1.Value++;
        //        for (b = dRand(0.0, 90.0), c = dRand(0.0, 360.0); (Math.Abs(c - 180.0) / 180.0 > Math.Sin(b * Math.PI / 180.0)); b = dRand(0.0, 90.0), c = dRand(0.0, 360.0)) ;
        //        c = 180.0 + (c - 180.0) / Math.Sin(b * Math.PI / 180.0);
        //        dllsub_co(ref A, ref b, ref c, out d, out m, out f, out g, out h, out i1, out k, out l);
        //        if (g > ThetaO - Aamp && g < ThetaO + Aamp) ListCo.Add(project(d, m, g, h, A, f, h)); //adding time_out_1 and phi_out_2 as the last two

        //    }
        //}
        //if (checkBox2.Checked)
        //{
        //    for (int j = 0; j < Prec1; j++)
        //    {
        //        progressBar1.Value++;
        //        for (b = dRand(0.0, 90.0), c = dRand(0.0, 360.0); (Math.Abs(c - 180.0) / 180.0 > Math.Sin(b * Math.PI / 180.0)); b = dRand(0.0, 90.0), c = dRand(0.0, 360.0)) ;
        //        c = 180.0 + (c - 180.0) / Math.Sin(b * Math.PI / 180.0);
        //        dllsub_counter(ref A1, ref b, ref c, out d, out m, out f, out g, out h, out i1, out k, out l);
        //        if (g > ThetaO1 - Aamp1 && g < ThetaO1 + Aamp1) ListCounter.Add(project(d, m, g, h, A1, f, h)); //adding time_out_1 and phi_out_2 as the last two

        //    }
        //}

        Parallel.Invoke(
            () =>
            {
                if (checkBox1.Checked)
                {
                    for (int i = 0; i < Prec; i++)
                    {
                        //progressBar1.Value++;
                        for (b = dRand(0.0, 90.0), c = dRand(0.0, 360.0); (Math.Abs(c - 180.0) / 180.0 > Math.Sin(b * Math.PI / 180.0)); b = dRand(0.0, 90.0), c = dRand(0.0, 360.0)) ;
                        c = 180.0 + (c - 180.0) / Math.Sin(b * Math.PI / 180.0);
                        dllsub_co(ref A, ref b, ref c, out d, out m, out f, out g, out h, out i1, out k, out l);
                        if (g > ThetaO - Aamp && g < ThetaO + Aamp) lock (ListCo) { ListCo.Add(project(d, m, g, h, A, f, h)); } //adding time_out_1 and phi_out_2 as the last two
                    }
                }
            },
                () =>
                {
                    if (checkBox2.Checked)
                    {
                        for (int j = 0; j < Prec1; j++)
                        {
                            //progressBar1.Value++;
                            for (q = dRand(0.0, 90.0), w = dRand(0.0, 360.0); (Math.Abs(w - 180.0) / 180.0 > Math.Sin(q * Math.PI / 180.0)); q = dRand(0.0, 90.0), w = dRand(0.0, 360.0)) ;
                            w = 180.0 + (w - 180.0) / Math.Sin(q * Math.PI / 180.0);
                            dllsub_counter(ref A1, ref q, ref w, out dd, out mm, out ff, out gg, out hh, out ii1, out kk, out ll);
                            if (gg > ThetaO1 - Aamp1 && gg < ThetaO1 + Aamp1) lock (ListCounter) { ListCounter.Add(project(dd, mm, gg, hh, A1, ff, hh)); } //adding time_out_1 and phi_out_2 as the last two
                        }
                    }
                });

        /* Reducing the quant time in both lists to convenient values and then calculating actual registration time */
        if (checkBox1.Checked)
        {
            mit = MinT(ListCo);
            for (int i = 0; i < ListCo.Count; i++) ListCo[i][2] = Time(ListCo[i][3], ListCo[i][2] - mit, A, true);
        }
        if (checkBox2.Checked)
        {
            mit = MinT(ListCounter);
            for (int i = 0; i < ListCounter.Count; i++) ListCounter[i][2] = Time(ListCounter[i][3], ListCounter[i][2] - mit, A1, false);
        }

        if (checkBox1.Checked)
        {
            mit = MinT(ListCo);
            for (int i = 0; i < ListCo.Count; i++) ListCo[i][2] -= mit;
        }
        if (checkBox2.Checked)
        {
            mit = MinT(ListCounter);
            for (int i = 0; i < ListCounter.Count; i++) ListCounter[i][2] -= mit;
        }

        if (checkBox1.Checked) TCo = MaxT(ListCo);
        if (checkBox2.Checked) TCounter = MaxT(ListCounter);

        /* Sorting both lists by registration time */
        if (checkBox1.Checked) ListCo.Sort(new QuantComparer());
        if (checkBox2.Checked) ListCounter.Sort(new QuantComparer());
        var spendTime = DateTime.Now - before;
        progressBar1.Visible = false;
        label5.Text = spendTime.ToString();
        //this.UnLockk();
        calcu = false;
    }
  • Вопрос задан
  • 158 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы