所以我啟動了一個對列表進行排序的線程,完成后它應該在我的控制器中調用“completedSorting()”方法?,F在我擔心僅僅調用該方法會導致該方法在另一個線程中執行。我相當擅長 C#,但線程對我來說是一個新概念。我只想返回線程,但是當我一次運行多個排序時,這只會導致混淆我 reccon,因此我希望他們調用“completedSorting”方法控制器: public void StartAlgorithm(Algorithm algorithm) { // check listOfArrays if (listManager.arrayList.Count == 0) { int[] newArray = CreateArray(algorithm.currentListLength); listManager.arrayList.Add(newArray); listManager.currentBiggestList = newArray.Length; Thread thread = new Thread(() => algorithm.SolveAlgorithm(newArray, algorithm)); thread.Start();冒泡排序: public override void SolveAlgorithm(int[] arr, Algorithm algorithm) { int temp = 0; for (int write = 0; write < arr.Length; write++) { for (int sort = 0; sort < arr.Length - 1; sort++) { if (arr[sort] > arr[sort + 1]) { temp = arr[sort + 1]; arr[sort + 1] = arr[sort]; arr[sort] = temp; } } } CompletedAlgorithmLog newlog = new CompletedAlgorithmLog(filteredArray, algorithm); this.controller.OnCompletedArray(newlog);最后一行是我需要更改一些代碼的地方,我認為我必須在執行 mainthread=>completedSorting 之類的操作后返回,但我不知道具體如何操作。
1 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
使用 Task Parallel 庫并使用 Task Continuation
System.Threading.Tasks.Task
.Run(() =>
{
StartAlgorithm(algorithm);
})
.ContinueWith(()=>{
CompletedAlgorithmLog newlog = new CompletedAlgorithmLog(filteredArray, algorithm);
this.controller.OnCompletedArray(newlog);
});
它將在與線程池不同的線程上運行算法,一旦完成,將在 continueWith 中執行代碼。
- 1 回答
- 0 關注
- 82 瀏覽
添加回答
舉報
0/150
提交
取消