I am creating an application using async-wait methods. But for me there is a big problem with using them. After reading several articles, I still don't know what is the best way to port my heavy synchronization operations to async methods.
I have 2 ideas. Which one is better?
1) Current implementation.
private Task<List<UploadedTestModel>> ParseTestFiles(List<string> filesContent) { var tcs = new TaskCompletionSource<List<UploadedTestModel>>(); Task.Run(() => { var resultList = new List<UploadedTestModel>(); foreach (var testBody in filesContent) { try { var currentCulture = Thread.CurrentThread.CurrentCulture; var serializerSettings = new JsonSerializerSettings { Culture = currentCulture }; var parsedData = JsonConvert.DeserializeObject<UploadedTestModel>(testBody, serializerSettings); resultList.Add(parsedData); } catch(Exception exception) { tcs.SetException(exception); } } tcs.SetResult(resultList); }); return tcs.Task; }
I use Task.Run and TaskCompletionSource
2) Using only Task.Run without TaskCompletionSource
private Task<List<UploadedTestModel>> ParseTestFiles(List<string> filesContent) { return Task.Run(() => { . . . . return resultList; }); }
c # asynchronous task-parallel-library
Egorikas
source share