DispatcherPriority

發布 多線程 0


代碼的目的是啟動3個task,通過調用C DLL中getSWMMout方法讀取同一二級制文件中不同部分,寫3個文件,NdeExtract和LinkExtract方法和SubExtract方法代碼結構一致。
    當兩個checkBox都選中時,報錯代碼:托管調試助手 "FatalExecutionEngineError":“運行時遇到了錯誤。此錯誤的地址為 0x6cb8f121,在線程 0x3f58 上。錯誤代碼為 0xc0000005。此錯誤可能是 CLR 中的 bug,或者是用戶代碼的不安全部分或不可驗證部分中的 bug。此 bug 的常見來源包括用戶對 COM-interop 或 PInvoke 的封送處理錯誤,這些錯誤可能會損壞堆棧。”。請問怎么解決呢?代碼如下:
// Extract subcatch output values if checked
     if (chkSub.IsChecked == true)
     {
     progressBar1.Value = 0;
     // define file path for subcatchment extraction
     string subcatchOutPath = "subcatch.txt";
     // instaniate a task for extraction
     var subTask = SubExtract(subcatchOutPath);
     }
     // Extract node output values if checked
     if (chkNode.IsChecked == true)
     {
     progressBar2.Value = 0;
     // define file path for node extraction
     string nodeOutPath = "node.txt";
     // instaniate a task for extraction
     var nodeTask = NodeExtract(nodeOutPath);
     }
     // Extract linke output values if checked
     if (chkLink.IsChecked == true)
     {
     progressBar3.Value = 0;
     // define file path for link extraction
     string linkOutPath = "link.txt";
     // instaniate a task for extraction
     var linkTask = LinkExtract(linkOutPath);
     }
     async Task SubExtract(string outPathSub)
     //
     // Purpose: extract variable outcome for subcatchment
     //
     {
     int errSubID;
     int cntSub = 0;
     int sumSubPrd = Nsubcatch * extNperiod; // total for subcatchment outcome
     float subValue = 0F; // outcome value for subcatchment extraction
     // create file stream for writing file
     FileStream fsSubcatch = new FileStream(outPathSub, FileMode.Create);
     StreamWriter swSubcatch = new StreamWriter(fsSubcatch);
     await Task.Run(() =>
     {
     for (int i = 0; i < Nsubcatch; ++i)
     {
     for (int j = 1; j <= extNperiod; ++j)
     {
     for (int k = 0; k < NsubcatchVar; ++k)
     {
     // getSWMMout為C DLL中方法
     errSubcatch = getSWMMresult(SUBCATCH, i, k, j, ref subValue);
     }
     // update progressbar and textBlock value
     cntSub++;
     progressBar1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
     (Action)delegate ()
     {
     progressBar1.Value = cntSub * 100 / sumSubPrd;
     });
     }
     }
     // empty buffer
     swSubcatch.Flush();
     // close stream
     swSubcatch.Close();
     fsSubcatch.Close();
     GC.Collect();
     });
     }
 
美国扑克50手返水