I need to implement asynchronous work of UI and algorithm in the program. I have already done something similar in the past, but then my algorithm did not return anything, but only worked with files. It looked like this:
async void CopyFiles(DirectoryInfo dir) { var len = await Task.Run(() => dir.GetFiles().Length); var p = new Progress<int>(i => { progress.Text = i + " файлов из " + len + " отсортировано"; pBar.PerformStep(); }); await Task.Run(() => RealCopyFiles(dir, p)); bDone.Visible = true; } void RealCopyFiles(DirectoryInfo dir, IProgress<int> progress) { var i = 0; foreach (var k in dir.GetFiles()) { string month = k.LastWriteTime.ToString("yyyy-MM"); //... i += 1; progress.Report(i); } } In CopyFiles I called RealCopyFiles , which reported progress. Now I need RealCopyFiles to return the value as well. Therefore, in the place where I call CopyFiles I need to assign the result of the RealCopyFiles method RealCopyFiles another variable. But no matter how hard I try, the compiler always swears that it is impossible to do this with asynchronous methods (I don’t mind, just I don’t know how to do it). So how can you reorganize this code, given that RealCopyFiles , and therefore CopyFiles must return a value?
Added flow example:
public int[,] getCrossCorrelation(byte[,] main_Image, byte[,] template_image) { //.. rowsCompleted++; progressBar1.Value = (int)(((double)rowsCompleted / height) * 100); //.. return crossCorelation; } From this method, I would like to bring the processing progress bar in a separate thread. But if we do it by analogy with the previous example, I cannot return the result of this method to an asynchronous method, which, in turn, should also return this result in the place where it is called
What I have at the moment:
public int[,] getCrossCorrelation(byte[,] main_Image, byte[,] template_image, IProgress<int> progress) { /.. rowsCompleted++; currentProgress = (int)(((double)rowsCompleted / height) * 100); progress.Report(currentProgress); /.. return crossCorelation; } async Task<int[,]> test() { var p = new Progress<int>(currentProgress => { progressBar1.Value = currentProgress; }); var value = await Task.Run(() => getCrossCorrelation(byteImageMain, byteImageTemplate, p)); return value; } private void btnStart_Click(object sender, EventArgs e) { /.. int[,] crossCorelation = test(); //<-- тут ошибка } Error: implicit type conversion "System.Threading.Tasks.Task" to "int [ , ]" is not possible.