Answer the question
In order to leave comments, you need to log in
The program closes when Parallel.For?
Hello, there is a program that calculates the trajectory of an object. Because of the long calculation, I used Parallel.For, but the program simply closed (most often without an error). Who knows what's wrong?
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";
}
Answer the question
In order to leave comments, you need to log in
Most likely trap in unmanaged code, which probably does not support multithreading. You can try to put a global lock on the dllsub() call to make sure.
Code style sucks.
first: never use a random class from multiple threads, it is non-thread-safe and sometimes (in practice at high frequency of generation) the instance "breaks" and starts always returning 0
second: the dllsub, project method remains a mystery, since they use external variables - this is important
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question