Beispiel #1
0
        private void btnBreak_Click(object sender, EventArgs e)
        {
            int    sum = 0;
            string msg = "";

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //引用stopwatch物件
            sw.Reset();                                                           //碼表歸零
            sw.Start();                                                           //碼表開始計時
            object sync = new object();

            Parallel.For(1, 11, (i, LoopState) =>
            {
                if (i == 5)
                {
                    LoopState.Break();
                }
                lock (sync) //若沒有Lock則會因競爭而有資料遺失
                {
                    sum = sum + i;
                }
                string buf = String.Format("i={0},sum={1}\n", i, sum);
                msg        = msg + buf;
            });
            sw.Stop();//碼錶停止
            string ParallelForResult = sw.Elapsed.TotalMilliseconds.ToString();

            rtxtResult.Text = string.Format("Break執行結束:{0}毫秒。\n{1}", ParallelForResult, msg);
        }
Beispiel #2
0
        public override void SimulateEnviroment()
        {
            Exception error = null;

            Parallel.For(0, Species.Length, (i, LoopState) =>
            {
                try
                {
                    Genome FinishedSpecie;
                    do
                    {
                        FinishedSpecie = Species[i].EvolveSpecie();
                        CheckBestCandidate(FinishedSpecie.SpecInfo.GetCopy());
                    } while (FinishedSpecie.BestCandidate.OffSet != 0);
                }
                catch (Exception e)
                {
                    error = e;
                    LoopState.Break();
                }
            });
            if (error != null)
            {
                ExceptionDispatchInfo.Capture(error).Throw();
            }
        }