private void button1_Click(object sender, EventArgs e) { var before = DateTime.Now; double a = 5; Random rand = new Random(); double d = 0;//theta1 double m = 0;//phi1 double f = 0; double g = 0;//theta2 double h = 0;//phi2 double i1 = 0; double k = 0; int l = 0; Parallel.For(0, 90, (ii) => { double i = ii; for (double j = 0; j < 360; j += 1 * 90 / (90 - i)) { dllsub(ref a, ref i, ref j, out d, out m, out f, out g, out h, out i1, out k, out l); if (g > 84 && g < 86) List.Add(project(d, m, g, h, 1000, Time(m - h, f, a))); } }); textBox2.Text = "Done; " + List.Count; var spendTime = DateTime.Now - before; label1.Text = spendTime.ToString(); } 

Hello, the problem is that when you try to use Parallel.for the form closes. How can this be fixed? That for was originally:

 for (double i = 0; i < 90; i += 1) { for (double j = 0; j < 360; j += 1 * 90/(90 - i)) { dllsub(ref a, ref i, ref j, out d, out m, out f, out g, out h, out i1, out k, out l); if (g > 84 && g < 86) List.Add(project(d, m, g, h, 1000, Time(m - h, f, a))); } } 

Here is the whole project:

 public partial class Form1 : Form { [DllImport("PVFProject6.dll", EntryPoint = "dllsub_", CallingConvention = CallingConvention.Cdecl)]//CharSet = CharSet.Unicode, static extern int dllsub(ref double r_0, ref double theta_0, ref double phi_0, out double theta_out1, out double phi_out1, out double time_out1, out double theta_out2, out double phi_out2, out double time_out2, out double Energy, out int Rc); List<double[]> List = new List<double[]>(); public Form1() { InitializeComponent(); } public double[] project(double theta1, double phi1, double theta2, double phi2, double a, double time) { double x1 = 10 * a * Math.Sin(theta1 * Math.PI / 180) * Math.Cos((phi1 - phi2) * Math.PI / 180); double y1 = 10 * a * Math.Sin(theta1 * Math.PI / 180) * Math.Sin((phi1 - phi2) * Math.PI / 180); double z1 = 10 * a * Math.Cos(theta1 * Math.PI / 180); double x2 = 11 * a * Math.Sin(theta2 * Math.PI / 180) * Math.Cos(0 * Math.PI / 180); double y2 = 11 * a * Math.Sin(theta2 * Math.PI / 180) * Math.Sin(0 * Math.PI / 180); double z2 = 11 * a * Math.Cos(theta2 * Math.PI / 180); double x = (x2 - 11 * a * (x2 - x1) / a) * Math.Cos(theta2 * Math.PI / 180); double y = y2 - 11 * a * (y2 - y1) / a; double[] A = new double[] { x, y, time }; return A; } public double Time(double phi1, double time1, double r0) { return time1 - phi1 / 360 * 2 * Math.PI * (Math.Pow(2 * r0, 3 / 2) + 0.9981); } public int MyCount(List<double[]> L) { int k = 0; bool inc = true; for (int i = 0; i < List.Count; i++) { for (int j = 0; j < List.Count; j++) if (L[j] == L[i] && i != j) inc = false; if (inc) k++; inc = true; } return k; } public void TimeSort(ref List<double[]> A) { double h = 0; for (int i = 1; i < A.Count; i++) { if (A[i][2] < A[i - 1][2]) { h = A[i][0]; A[i][0] = A[i - 1][0]; A[i - 1][0] = h; h = A[i][1]; A[i][1] = A[i - 1][1]; A[i - 1][1] = h; h = A[i][2]; A[i][0] = A[i - 1][2]; A[i - 1][2] = h; } } } private void button1_Click(object sender, EventArgs e) { var before = DateTime.Now; double a = 5; Random rand = new Random(); double d = 0;//theta1 double m = 0;//phi1 double f = 0; double g = 0;//theta2 double h = 0;//phi2 double i1 = 0; double k = 0; int l = 0; Parallel.For(0, 90, (ii) => { double i = ii; for (double j = 0; j < 360; j += 0.1 * 90 / (90 - i)) { dllsub(ref a, ref i, ref j, out d, out m, out f, out g, out h, out i1, out k, out l); if (g > 84 && g < 86) List.Add(project(d, m, g, h, 1000, Time(m - h, f, a))); } }); textBox2.Text = "Done; " + List.Count; var spendTime = DateTime.Now - before; label1.Text = spendTime.ToString(); } private void button2_Click(object sender, EventArgs e) { this.Refresh(); textBox2.Text = "" + MyCount(List); richTextBox1.Text = ""; foreach (double[] a in List) richTextBox1.Text = richTextBox1.Text + (int)Math.Round(a[1] * 100 + 800) + "\t" + (int)Math.Round(a[0] * 100 + 300) + "\t" + Math.Round (a[2]) + "\n"; } private void Form1_Paint(object sender, PaintEventArgs e) { foreach (double[] a in List) e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(40, 0, 130, 180)), (int)Math.Round(a[1] * 50 + 300), (int)Math.Round(a[0] * 4000 + 300), 8, 8); } } 

dllsub - procedure from the library written in fortran'e

  • dllsub , project ?? List - thread safe? - Igor
  • Actually, here's the answer for you: lock(List) { List.Add(...); } lock(List) { List.Add(...); } - Igor
  • Thanks for the help! - Wingo

0