Portanto, estou iniciando um thread que classifica uma lista, após a conclusão, ele deve chamar o método 'completedSorting ()' em meu controlador. Agora, temo que apenas chamar o método fará com que o método seja executado na outra thread. Eu sou decentemente bom em C #, mas threading é um novo conceito para mim.
Eu apenas retornaria o thread, mas quando eu executo várias classificações ao mesmo tempo, isso só levará a confuson I reccon, portanto, eu quero que eles chamem o método 'completedSorting'
controlador:
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();
Tipo de bolha:
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);
a última linha é onde preciso alterar algum código, reconheço que teria que retornar depois de fazer algo como mainthread => completedSorting, mas não sei exatamente como.
Use a biblioteca Paralela de Tarefas e use a Continuação de Tarefas
System.Threading.Tasks.Task
.Run(() =>
{
StartAlgorithm(algorithm);
})
.ContinueWith(()=>{
CompletedAlgorithmLog newlog = new CompletedAlgorithmLog(filteredArray, algorithm);
this.controller.OnCompletedArray(newlog);
});
ele executará o agoritmo em um thread diferente do threadpool e, uma vez concluído, executará o código dentro de continueWith.
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras