0

сделал программу работающую в нескольких потоках, но проблема в том что она сама закрывается после того как progressbar заполняется, что делать

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Monte_Carlo
{
    delegate void InvokeDelegate(int progress);

    public partial class Form1 : Form
    {

        static List<double> n_circle_List = new List<double>();
        private void Method_Monte_Karlo()
        {
            double _num;
            _num = int.Parse(textBox2.Text)/4;
            int n_point = (int)_num;
            double n_circle = 0;
            double n_point_count = n_point;
            double count= n_point;
            int progress;
            Random r = new Random();
            for (int i = 0; i < n_point; i++)
            {
                if (IsPointInCircle(1.0, r.NextDouble(), r.NextDouble()))
                {
                    n_circle++;
                }
                progress = (int)((n_point_count / (count)) * 100);
                this.Invoke(new InvokeDelegate(updateChart), progress);
                Thread.Sleep(1);
                n_point_count++;
            }
            n_circle_List.Add(n_circle);
        }
        private void updateChart(int progress)
        {
            progressBar1.Value = progress; 
            Thread.Sleep(1);

        }
        static bool IsPointInCircle(double R, double x, double y)
        {
            return ((x * x + y * y) <= R * R);
        }
        static void Threads(int numThreads, int num)
        {
            ThreadPool.SetMaxThreads(numThreads, 0);
            for (int i = 0; i < numThreads; i++)
            {
                Thread.Sleep(20);
            }

        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int _num;
            double pi = 0;
            double j = 0;
            _num = int.Parse(textBox2.Text);
            if (progressBar1.Value >= 100)
            {
                for (int i = 0; i < n_circle_List.Count; i++)
                {
                    j += n_circle_List[i];
                }
                pi = 4 * j / _num;
                textBox6.Text = pi.ToString();
                n_circle_List.Clear();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int _num;
            double pi = 0;
            double j = 0;
            _num = int.Parse(textBox2.Text);
            Thread t1 = new Thread(new ThreadStart(Method_Monte_Karlo));        
            Thread t2 = new Thread(new ThreadStart(Method_Monte_Karlo));        
            Thread t3 = new Thread(new ThreadStart(Method_Monte_Karlo));     
            Thread t4 = new Thread(new ThreadStart(Method_Monte_Karlo));
            t1.IsBackground = true;
            t1.Start();
            t2.IsBackground = true;
            t2.Start();
            t3.IsBackground = true;
            t3.Start();
            t4.IsBackground = true;
            t4.Start();
        }  
    }
}
  • Запускайте приложение в режиме отладки (Debug), скорее всего возникает исключение, а вы его не видите. – aepot Jun 06 '20 at 16:16
  • Вот еще посмотрите это – aepot Jun 06 '20 at 16:19

0 Answers0