Closes the program in Parallel.For?

Hello, there is a program that calculates the trajectory of the object. Due to the long calculation used Parallel.For, but the program was simply shut down (often without errors). Who knows what is the error?
private void выполнитьРасчетToolStripMenuItem1_click(object sender, EventArgs e)
{
 //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
 Random rand = new Random();
 var ob = new object();
 var before = DateTime.Now;
 List = new List<double[]>();
 Parallel.For(0, Prec, val =>
{
 double b;
 double c;
 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;
 for (b = rand.Next() % 900 / 10.0, c = rand.Next() % 3600 / 10.0; (Math.Abs(c - 180.0) / 180.0 > Math.Sin(b * Math.PI / 180.0)); b = rand.Next() % 900 / 10.0, c = rand.Next() % 3600 / 10.0) ;
 c = 180.0 + (c - 180.0) / Math.Sin(b * Math.PI / 180.0);
 dllsub(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 (List)
{
 List.Add(project(d, m, g, h, A, f, h));
}
}
});
 double mit = MinT(List);
 for (int i = 0; i < List.Count; i++) List[i][2] = Time(List[i][3], List[i][2] - mit, A);
 List.Sort(new QuantComparer());
this.Refresh();
 foreach (double[] a1 in DrawL) richTextBox1.Text = richTextBox1.Text + (int)Math.Round(a1[0]) + "\t" + (int)Math.Round(a1[1]) + "\t" + Math.Round(a1[2]) + "\n";
 }</double[]>

Sometimes it POPs up this error: Attempted to read or write protected memory. This often indicates that other memory is corrupt.
In the output window writes all unclear, except this: The program '[7132] WindowsFormsApplication2.vshost.exe: Managed (v4.0.30319)' has exited with code 127 (0x7f).
dllsub - procedure for fortran'e, from which we get the coordinates of the body. We record them in a list.
July 8th 19 at 15:26
2 answers
July 8th 19 at 15:28
Solution
Probably trahaetsya in unmanaged code, which likely does not support multithreading. You can try to put a global lock on the call dllsub () to be sure.
Coding style sucks.
Yes, the program crashes, and the processor is loaded on 100 - forest_Roberts commented on July 8th 19 at 15:31
So no luck to parallelize :( - Camille_Fee commented on July 8th 19 at 15:34
you try to break into independent groups of calculations have parallel.the odds have overworked method for sharing context - Ena_Luettg commented on July 8th 19 at 15:37
Considering the 100% download percent, we can assume that the calculations are enough rasparallelivanija and additional efforts of the special use will not bring - Camille_Fee commented on July 8th 19 at 15:40
July 8th 19 at 15:30
first: never use the random class from multiple threads, and sometimes it non-thread-safe (in practice, when the frequency of generation), the instance of "breaks" and starts to always give 0
second: it remains a mystery method dllsub, project, because they use external variables is important
Here is the project:
public double[] project(double theta1, double phi1, double theta2, double phi2, double a, double time, double phi)
{
double x1 = 200000 * a * Math.Sin(theta1 * Math.PI / 180) * Math.Cos((phi1 - phi2) * Math.PI / 180);
double y1 = 200000 * a * Math.Sin(theta1 * Math.PI / 180) * Math.Sin((phi1 - phi2) * Math.PI / 180);
double z1 = 200000 * a * Math.Cos(theta1 * Math.PI / 180);

double x2 = 220000 * a * Math.Sin(theta2 * Math.PI / 180);
double y2 = 0;
double z2 = 220000 * a * Math.Cos(theta2 * Math.PI / 180);

double x = (x1 * y2 - y1 * x2) / (x1 - x2);
double y = (x1 * z2 - x2 * z1) / (x1 - x2);
double[] A = new double[] { x, y, time, phi };
return A;
}
Connection library:
[DllImport("PVFProject6.dll", EntryPoint = "dllsub_", CallingConvention = CallingConvention.Cdecl)]
static extern int dllsub(r_0, ref double, ref double theta_0, phi_0 ref double, out double theta_out1, phi_out1 out double, out double time_out1,
theta_out2 out double, out double phi_out2, time_out2 out double, out double Energy, out int Rc); - forest_Roberts commented on July 8th 19 at 15:33
mark field, A volitile - Camille_Fee commented on July 8th 19 at 15:36
Error 1 'WindowsFormsApplication2.Form1.A': a volatile field cannot be of the type 'double' C:\Users\Wingo\Desktop\WindowsFormsApplication2111\WindowsFormsApplication2\Form1.cs 24 25 WindowsFormsApplication2 - Ena_Luettg commented on July 8th 19 at 15:39
: Ah, Yes...
then it is necessary to read/write using the Interlocked methods.Read and Volatile.Write - Camille_Fee commented on July 8th 19 at 15:42
it is necessary to apply to the value of the pointer ref A, most likely from multiple threads is recorded - forest_Roberts commented on July 8th 19 at 15:45

Find more questions by tags C#