Неправильно считает при использовании метода Thread - C#

Узнай цену своей работы

Формулировка задачи:

При выполнении выдаёт ошибку " Индекс находился вне границ массива.". Хотя насколько я понимаю он не должен выходить за рамки массива. Подскажите пожалуйста в чем проблема?
using System;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace testparallel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
              
        double I;
        
        private void button1_Click_1(object sender, EventArgs e)
        {
            string a_s = textBox1.Text,
                   b_s = textBox2.Text,
                   n_s = textBox3.Text;
 
            int a = Convert.ToInt32(a_s),
                b = Convert.ToInt32(b_s),
                n = Convert.ToInt32(n_s);
 
            double[] x;
            x = new double[n+1];
 
            double h = (b - a) / n;
 
            for (int i = 0; i <= n; i++)
            {
                x[i] = a + (i * h);  
            }
            textBox6.Clear();
            integral(x, n);
 
        }
 
        public void integral(double [] x, int n)
        {
            Stopwatch st = new Stopwatch();
            st.Start();
                
            for (int i = 1; i <= n; i++)
            {
                I += ((x[i] * x[i]) / 2.0) - ((x[i - 1] * x[i - 1]) / 2.0);
                
            }           
            st.Stop();
            TimeSpan ts = st.Elapsed;
            string s = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
        label1.Text = Convert.ToString(s);
            textBox4.Text += Convert.ToString(I);
        }
        
        double S=0;
 
        private void button2_Click(object sender, EventArgs e)
        {
            string a_s = textBox1.Text,
                   b_s = textBox2.Text,
                   n_s = textBox3.Text;
 
            int a = Convert.ToInt32(a_s),
                b = Convert.ToInt32(b_s),
                n = Convert.ToInt32(n_s);
 
            double[] x;
            x = new double[n];
 
            double h = (b - a) / n;
 
            for (int i = 0; i <= n; i++)
            {
                x[i] = a + (i * h);
            }
            textBox6.Clear();
            int_p(x,n);
            textBox6.Text = Convert.ToString(S);
        }
 
        public void int_p(double[] x, int n)
        {
            int g = x.Length;
            int t = n;
 
            Thread[] th = new Thread[n + 1];
 
            Stopwatch st = new Stopwatch();
            st.Start();
            for (int j = 1; j <= n; j++)
            {
                th[j] = new Thread(() => {
 
                    S += ((x[j] * x[j]) / 2.0) - ((x[j - 1] * x[j - 1]) / 2.0);
                });
            }
            for (int j = 1; j <= n; j++)
                th[j].Start();
 
            for (int j = 1; j <= n; j++)
                th[j].Join();
            st.Stop();
            TimeSpan ts = st.Elapsed;
            string s = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            label2.Text = Convert.ToString(s);
        }
    }
}

Решение задачи: «Неправильно считает при использовании метода Thread»

textual
Листинг программы
for (int i = 0; i < n; i++)

Оцени полезность:

13   голосов , оценка 3.846 из 5
Похожие ответы