I have multiple running tasks in Console application. In my task I call some method from EWS Managed API. So When EWS throw exception code execution is not return to WaitAll.
while (index < messageData.Count)
{
var mess = messageData[index];
var task = new System.Threading.Tasks.Task<EventsCreationResult>(() =>
{
EventsCreationResult taskResult = null;
taskResult = CreateEmail(mess, Service);
return taskResult;
});
task.Start();
tasks.Add(task);
index = index + 1;
}
System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
And Function
public static EventsCreationResult CreateEmail(string folderName, EmailMessage mess)
{
EventsCreationResult result = new EventsCreationResult();
result.Key = mess.Subject;
try
{
mess.Save(FindFolder(folderName).Id);
result.EventsCreationLog = "Message created:" + mess.Subject;
}
catch (Exception ex)
{
result.Error = ex;
}
return result;
}
So how to avoid dead lock and if its possible do not cancel task which not rise error.
Related
I'd like to spawn some threads and in each thread sequentially make calls to an API and aggregate the results (some sort of stress testing). Here is my attempt:
private async Task DoWork()
{
var allResponses = new List<int>();
for (int i = 0; i < 10; i++)
{
await Task.Run(() =>
{
var responses = Enumerable.Range(0, 50).Select(i => CallApiAndGetStatusCode());
allResponses.AddRange(responses);
});
}
// do more work
}
private int CallApiAndGetStatusCode()
{
try
{
var request = new HttpRequestMessage(httpMethod.Get, "some url");
var responseResult = httpClient.SendAsync(request).Result;
return (int)responseResult.StatusCode;
}
catch (Exception e)
{
logger.LogError(e, "Calling API failed");
}
}
However, this code always ends up the catch with the inner exception being {"A task was canceled."}. What am I doing wrong here?
There is no benefit to using either Enumerable.Range or .AddRange in your example, since you do not need the seeded number. Your code must be converted to async/await to avoid deadlocks and in doing so, you can simply loop inside of each task and avoid any odd interactions between Enumerable.Select and await:
private async Task DoWork()
{
var allTasks = new List<Task>(10);
var allResponses = new List<int>();
for (int i = 0; i < 10; i++)
{
allTasks.Add(Task.Run(async () =>
{
var tempResults = new List<int>();
for (int i = 0; i < 50; i++)
{
var result = await CallApiAndGetStatusCode();
if (result > 0) tempResults.Add(result);
}
if (tempResults.Count > 0)
{
lock (allResponses)
{
allResponses.AddRange(tempResults);
}
}
}));
}
await Task.WhenAll(allTasks);
// do more work
}
private async Task<int> CallApiAndGetStatusCode()
{
try
{
var request = new HttpRequestMessage(HttpMethod.Get, "some url");
var responseResult = await httpClient.SendAsync(request);
return (int)responseResult.StatusCode;
}
catch (Exception e)
{
logger.LogError(e, "Calling API failed");
}
return -1;
}
Note that this code is overly protective, locking the overall batch before adding the temp results.
I changed your code to this and work
async Task DoWork()
{
var allResponses = new List<int>();
for (int i = 0; i < 10; i++)
{
await Task.Run(() =>
{
var responses = Enumerable.Range(0, 3).Select(i => CallApiAndGetStatusCodeAsync());
allResponses.AddRange(responses.Select(x => x.Result));
});
}
// do more work
}
async Task<int> CallApiAndGetStatusCodeAsync()
{
try
{
var request = new HttpRequestMessage(HttpMethod.Get, "http://www.google.com");
var responseResult = await httpClient.SendAsync(request);
return (int)responseResult.StatusCode;
}
catch (Exception e)
{
logger.LogError(e, "Calling API failed");
return -1;
}
}
public async Task<JobViewModel> Handle(AddJobCommand command, CancellationToken cancellationToken)
{
if (command.JobViewModel == null) throw new InvalidOperationException("Empty request.");
var jobViewModel = command.JobViewModel;
try
{
var job = _mapper.Map<DataAccess.Domain.Lab.Job>(jobViewModel);
_context.Set<DataAccess.Domain.Lab.Job>().Add(job);
if (job.Notes!= null)
{
var newNote = job.Notes.FirstOrDefault(n => n.IsNew);
if (newNote != null)
{
newNote.JobId = job.Id;
_context.Set<DataAccess.Domain.Lab.JobNote>().Attach(newNote);
_context.Entry(newNote).State = EntityState.Added;
}
}
await OnBeforeAdd(job); // job.Name = await GenerateJobName();
await _context.SaveChangesAsync();
jobViewModel.Id = job.Id;
return jobViewModel;
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
catch (DbUpdateException e)
{
Console.WriteLine(e.Message);
throw;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
protected async Task OnBeforeAdd(DataAccess.Domain.Lab.Job job)
{
if (string.IsNullOrWhiteSpace(job.Name))
{
job.Name = await GenerateJobName();
}
}
private async Task<string> GenerateJobName()
{
Func<int, int, string> composeName = (year, number) => string.Format("S{0:D2}{1:D3}", year, number);
int currentYear = DateTime.UtcNow.Year % 100;
int newJobNumber = 501;
//if (_context.Jobs!= null)
// {
// string maxJobNumber = await _context.Jobs.MaxAsync(j => j.Name);
string maxJobNumber = await _context.Jobs.MaxAsync(j=>j.Name);
if (!string.IsNullOrWhiteSpace(maxJobNumber))
{
var matches = Regex.Matches(maxJobNumber, "S" + currentYear + "(?<num>[0-9]{3})");
if (matches.Count == 1)
{
newJobNumber = int.Parse(matches[0].Groups["num"].Value) + 1;
}
}
// }
string newName = composeName(currentYear, newJobNumber);
while (await _context.Jobs.AnyAsync(j => j.Name == newName))
{
newJobNumber++;
newName = composeName(currentYear, newJobNumber);
}
return newName;
}
}
}
Below line give error:
string maxJobNumber = await _context.Jobs.MaxAsync(j=>j.Name);
On running the code it throws below exception "A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe."
Also the same line "string maxJobNumber = await _context.Jobs.MaxAsync(j=>j.Name);" shows warning message as
converting null literal or possible null value to non-nullable type string error . Can this be an issue?
Some comments about the above code:
There's no need for these lines:
if (job.Notes!= null)
{
var newNote = job.Notes.FirstOrDefault(n => n.IsNew);
if (newNote != null)
{
newNote.JobId = job.Id;
_context.Set<DataAccess.Domain.Lab.JobNote>().Attach(newNote);
_context.Entry(newNote).State = EntityState.Added;
}
}
Once you've added the job to the DbSet, EF will automatically figure out that its JobNotes need to be added as well. And, it will determine there state appropriately.
This is the pattern I would use:
public async Task<JobViewModel> Handle(AddJobCommand command, CancellationToken cancellationToken)
{
var jobViewModel = command.JobViewModel;
var job = _mapper.Map<DataAccess.Domain.Lab.Job>(jobViewModel);
job.Name = await GenerateJobName(job.Name);
await _context.Set<DataAccess.Domain.Lab.Job>().AddAsync(job);
await _context.SaveChangesAsync();
jobViewModel.Id = job.Id;
return jobViewModel;
}
private async Task<string> GenerateJobName(DataAccess.Domain.Lab.Job job)
{
if (!string.IsNullOrWhiteSpace(job.Name)) return job.Name;
...
}
You may be overthinking all the try-catch stuff. It is more than half the code and is obscuring the real logic. I would move that out to a higher location and concentrate on making this do the one thing it's intended to do.
The reason the code performs differently in PROD is due to a race condition. There's no debugger in PROD, so your first calls are finishing with enough time to start the second calls.
I want to do 4 actions and each one can throw an exception. I need to execute them all and log all errors that happened along the way
I can do it in a big and clumsy way :
var exceptions = new List<ExceptionType1>();
try {
result1 = await action1();
} catch (ExceptionType1 ex1) {
exceptions.Add(ex1);
logger.Log(ex1);
}
try {
result2 = await action2();
} catch (ExceptionType1 ex1) {
exceptions.Add(ex1);
logger.Log(ex1);
}
try {
result3 = await action3(result1);
} catch (ExceptionType2 ex2) {
var ex1 = new ExceptionType1(ex2);
exceptions.Add(ex1);
logger.Log(ex1);
}
try {
result4 = await action4(result2);
} catch (ExceptionType2 ex2) {
var ex1 = new ExceptionType1(ex2);
exceptions.Add(ex1);
logger.Log(ex1);
}
if (exceptions.Count > 0) {
return false;
}
Obviously sometimes things like that bloat in size quickly
I would like to do it in more elegant way :
var exceptions = new List<ExceptionType1>();
try {
result1 = await action1();
result2 = await action2();
result3 = await action3(result1);
result4 = await action4(result2);
} catch(Exception ex) {
var ex1 = new ExceptionType1(ex);
exceptions.Add(ex1);
logger.Log(ex1);
???
~~Goto try block and continue executing~~
???
}
if (exceptions.Count > 0) {
return false;
}
I tried to find info how to get back into try block and continue executing but was unsuccessfull. Is it possible. Is there any other way around the problem that I didn't consider?
One way I guess (given your reequipments) is to create a local method
var exceptions = new List<Exception>();
async Task<T> DoAsync<T>(Func<Task<T>> func)
{
try
{
return await func();
}
catch (Exception ex)
{
exceptions.Add(ex);
logger.Log(ex);
}
return default;
}
result1 = await DoAsync(action1);
result2 = await DoAsync(action2);
if (exceptions.Count > 0)
{
return false;
}
Maybe you can extract this into a local function:
// declare this inside the method that contains the code you showed
async void GetResult<TResult, TException>(out TResult result, Func<Task<TResul>> resultGetter, Func<TException, ExceptionType1> mapper)
where TException: Exception {
try {
result = await resultGetter();
} catch (TException ex) {
var ex1 = mapper(ex)
exceptions.Add(ex1);
logger.Log(ex1);
}
}
Callers:
GetResult(out result1, action1, (ExceptionType1 e) => e);
GetResult(out result2, action2, (ExceptionType1 e) => e);
GetResult(out result3, async () => await action3(result1), (ExceptionType2 e) => new ExceptionType1(e));
GetResult(out result4, async () => await action4(result2), (ExceptionType2 e) => new ExceptionType1(e));
how to get back into try block and continue executing.. Is it possible?
You can't do what you are looking for: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch
The block is executed until an exception is thrown or it is completed successfully.
Once out of the block, even the variables that have been initialized are rolled back.
It's the whole point of the exception to stop the execution of the safe guarded code.
Follow the recommendations of the other two answers to simplify your code
I think WhenAll() method for Task will suit your purpose:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch#taskwhenall-example
First of all, your actions are already async. Moreover, you can divide to several WhenAll() blocks if you need to follow any order of execution
Example from docs:
public async Task action1()
{
throw new InvalidOperationException();
}
public async Task action2()
{
throw new NotImplementedException();
}
public async Task action3()
{
throw new InvalidCastException();
}
public async Task DoMultipleAsync()
{
Task theTask1 = action1();
Task theTask2 = action2();
Task theTask3 = action3();
Task allTasks = Task.WhenAll(theTask1, theTask2, theTask3);
try
{
await allTasks;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
Console.WriteLine("Task IsFaulted: " + allTasks.IsFaulted);
foreach (var inEx in allTasks.Exception.InnerExceptions)
{
Console.WriteLine("Task Inner Exception: " + inEx.Message);
}
}
}
// Output:
// Exception: Operation is not valid due to the current state of the object.
// Task IsFaulted: True
// Task Inner Exception: Operation is not valid due to the current state of the object.
// Task Inner Exception: The method or operation is not implemented.
// Task Inner Exception: Specified cast is not valid.
I have a ImageProcessor class that creates a list of tasks for each of the image providers, inside of these tasks I then run a parallel.foreach loop for all the images for each provider, I want to be able to cancel all the tasks and nested parallel loops from the console, I've found example of how to cancel tasks and how to cancel parallel loops, but I am unsure as how to do it for nested processes.
Below is the code I have at the moment
From my console app:
using (IUnityContainer container = Bootstrapper.Initialise())
{
IImageProcessor processor = container.Resolve<IImageProcessor>();
processor.Container = container;
try
{
InputArguments arguments = new InputArguments(args);
if (arguments.Contains("fs"))
{
processor.Initialise(arguments["fs"]);
}
else
{
processor.Initialise();
}
processor.Run();
Console.WriteLine("\r\n\n\nPress any key to Exit");
Console.ReadLine();
return (int)ExitCode.Success;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("\r\n\n\nPress any key to Exit");
Console.ReadLine();
return (int)ExitCode.UnknownError;
}
}
The Run method
public void Run()
{
List<Task> tasks = new List<Task>();
foreach (IFileService fileservice in this.fileServices)
{
Task task = Task.Factory.StartNew((arg) =>
{
IFileService fs = (IFileService)arg;
string msg = $"Processing {fs.ToString()}...";
FileLogger.Write(msg, fs.ToString());
ConsoleLogger.WriteLine(msg);
fs.ProcessFiles();
//fileservice.ReprocessUnMatchedData();
}, fileservice);
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
}
and inside each file service I have call this method:
protected bool ProcessFileRecord<T>() where T : IDataRecord
{
int matched = 0;
int notMatched = 0;
int skipped = 0;
bool result;
object lockObject = new object();
try
{
processTracker = GetTracker();
if (databaseHelper.TrackerFullyProcessed(processTracker))
{
LoggingService.Write("File already processed... Skipping.", LoggingTarget.All, serviceName);
result = true;
}
LoggingService.Write($"\r\nProcessing index file {fileRecord.IndexFileName}", LoggingTarget.File, serviceName);
Parallel.ForEach(
fileRecord.DataRecords,
new ParallelOptions() { MaxDegreeOfParallelism = Thread.CurrentThread.ManagedThreadId },
(item) =>
{
switch ((RecordProcessResult)ProcessRecord(item))
{
case RecordProcessResult.Invalid:
break;
case RecordProcessResult.Matched:
Increment(ref matched);
break;
case RecordProcessResult.NotMatched:
Increment(ref notMatched);
break;
case RecordProcessResult.Skipped:
Increment(ref skipped);
break;
default:
break;
}
lock (lockObject)
{
if ((matched + notMatched + skipped) % 100 == 0)
{
LoggingService.Write($"\tMatched: {matched}\tNot Matched: {notMatched}\tSkipped: {skipped}\t total: {matched + notMatched + skipped}", LoggingTarget.Trace & LoggingTarget.Console, serviceName);
}
}
});
LoggingService.Write($"Total Lines: {matched + notMatched + skipped} \r\nMatched: {matched} \r\nNot Matched: {notMatched} \r\nSkipped: {skipped}", LoggingTarget.All, serviceName);
this.databaseHelper.UpdateTracker(this.processTracker, matched, notMatched);
result = true;
}
catch (Exception ex)
{
LoggingService.Write($"Error processing data file:{fileRecord.IndexFileName}", LoggingTarget.All, serviceName);
LoggingService.Write($"{ex.ExceptionTreeAsString()}", LoggingTarget.All, serviceName);
LoggingService.Write($"Total Lines: {(matched + notMatched + skipped)} \r\nMatched: {matched} \r\nNot Matched: {notMatched} \r\nSkipped: {skipped}", LoggingTarget.All, serviceName);
this.databaseHelper.UpdateTracker(this.processTracker, matched, notMatched);
result = false;
}
return result;
}
Thank.
Please, look an example on this page
CancellationTokenSource
I have a list of task and few task are dependent on others, I would like to run the tasks now and if any of the task fails during execution, need to stop all the running task and close the application.
How can do this with TPL?
How to stop the running task?
I need to optimize the below code.
Detailed Requirement
- Start the Logon screen as a task.
o Run all other task in parallel only if the Logon is successful.
o Exit application in case of Logon failure or Cancel
- Exit application if any of the task fails
var done = new List<TaskDetails>();
var executing = new List<TaskDetails>();
var unblocked = new List<TaskDetails>();
var blocked = new List<TaskDetails>();
foreach (var startupTest in startupTests) {
if (startupTest.DependsOn == null) {
unblocked.Add(startupTest);
} else {
blocked.Add(startupTest);
}
}
IDictionary<int, TaskDetails> tasksByID = new Dictionary<int, TaskDetails>();
var tasksTPL = new Task<object>[startupTests.Count];
var taskCount = 0;
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
while (done.Count < startupTests.Count) {
while (executing.Count < config.MaximumConcurrency && unblocked.Count > 0) {
TaskDetails nextTask = unblocked[0];
lock (syncLock) {
unblocked.Remove(nextTask);
executing.Add(nextTask);
}
// Execute
try {
var method = GetMethod(
nextTask.AssemblyName, nextTask.ClassName, nextTask.MethodName
);
if (method == null) {
throw new Exception("Method" + nextTask.MethodName + " not available.");
}
tasksTPL[taskCount] =
Task<object>.Factory.StartNew(() => method.Invoke(null, null),
cancellationToken);
tasksByID.Add(tasksTPL[taskCount].Id, nextTask);
tasksTPL[taskCount].ContinueWith(tsk => {
lock (syncLock) {
done.Add(tasksByID[tsk.Id]);
executing.Remove(tasksByID[tsk.Id]);
}
if (tsk.Exception != null) {
TraceAlways(
"Caught Exception while running startuptest: " +
tsk.Exception
);
}
});
taskCount++;
} catch (TargetInvocationException e) {
TraceAlways(
"Failed running " + nextTask.MethodName + " method." + e.Message);
}
}
Task.WaitAny(tasksTPL.Where(task => task != null).ToArray());
var toRemove = new List<TaskDetails>();
lock (syncLock) {
List<string> doneTaskName =
done.Select(TaskDetails => TaskDetails.Name).ToList();
foreach (var task in blocked) {
bool isBlocked = task.DependsOn.Any(dep => !doneTaskName.Contains(dep));
if (!isBlocked) {
toRemove.Add(task);
unblocked.Add(task);
}
}
foreach (var TaskDetails in toRemove) {
blocked.Remove(TaskDetails);
}
}
if (executing.Count == 0 && unblocked.Count == 0 && blocked.Count > 0) {
throw new Exception("Cyclic Dependency");
}
}
taskCount = 0;
foreach (var task in tasksTPL) {
if (
(task.Status != TaskStatus.Faulted) &&
(task.Result is bool) &&
(!(bool)task.Result)
) {
TraceAlways("Startup Test" + startupTests[taskCount].MethodName + " failed.");
if (startupTests[taskCount].ShowNotification) {
cancellationTokenSource.Cancel();
MessageBox.Show(
"An error has accoured. See log for more details.", "Startup Error"
);
}
Environment.Exit(0);
break;
}
taskCount++;
}
Below is how I would implement it conceptually (if I understood the question correctly), although I did not attempt to meet all of your detailed requirements.
The code doesn't use ContinueWith.
DoTaskAsync is an individual independent task.
DoTaskSequenceAsync is a sequence of tasks, where some tasks depend on the result of the others.
BadTaskAsync is an example of the throwing tasks, its failure should cancel all other pending tasks.
WrapAsync wraps a task with try/catch to catch the task's exceptions and raise the global cancellation from inside.
All tasks also support the global cancellation from outside.
You can compile and try it as a console app.
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
namespace MultipleTasks
{
class Program
{
class Worker
{
// a single async Task
async Task<object> DoTaskAsync(string id, CancellationToken token, int delay)
{
Console.WriteLine("Task: " + id);
await Task.Delay(delay, token); // do some work
return id;
}
// DoTaskSequenceAsync depends on Task1, Task2, Task3
async Task<object> DoTaskSequenceAsync(string id, CancellationToken token)
{
Console.WriteLine("Task: " + id);
await DoTaskAsync(id + "." + "Task1", token, 1000);
await DoTaskAsync(id + "." + "Task2", token, 2000);
await DoTaskAsync(id + "." + "Task3", token, 3000);
// do more
return id;
}
// a bad task which throws
async Task<object> BadTaskAsync(string id, CancellationToken token, int delay)
{
Console.WriteLine("Task: " + id);
await Task.Delay(delay, token);
throw new ApplicationException(id);
}
// wraps a task and requests the cancellation if the task has failed
async Task<T> WrapAsync<T>(CancellationTokenSource cts,
Func<CancellationToken, Task<T>> taskFactory)
{
try
{
return await taskFactory(cts.Token);
}
catch
{
if (!cts.IsCancellationRequested)
{
cts.Cancel(); // cancel the others
}
throw; // rethrow
}
}
// run all tasks
public async Task DoWorkAsync(CancellationToken outsideCt)
{
var tasks = new List<Task<object>>();
var cts = new CancellationTokenSource();
ExceptionDispatchInfo capturedException = null;
try
{
using (outsideCt.Register(() => cts.Cancel()))
{
// these tasks run in parallel
tasks.Add(WrapAsync(cts, (token) => DoTaskAsync("Task1", token, 500)));
tasks.Add(WrapAsync(cts, (token) => DoTaskSequenceAsync("Sequence1", token)));
tasks.Add(WrapAsync(cts, (token) => DoTaskAsync("Task2", token, 1000)));
tasks.Add(WrapAsync(cts, (token) => BadTaskAsync("BadTask", token, 1200)));
tasks.Add(WrapAsync(cts, (token) => DoTaskSequenceAsync("Sequence2", token)));
tasks.Add(WrapAsync(cts, (token) => DoTaskAsync("Task3", token, 1500)));
await Task.WhenAll(tasks.ToArray());
}
}
catch (Exception e)
{
capturedException = ExceptionDispatchInfo.Capture(e);
}
if (outsideCt.IsCancellationRequested)
{
Console.WriteLine("Cancelled from outside.");
return;
}
if (cts.IsCancellationRequested || capturedException != null)
{
if (cts.IsCancellationRequested)
{
Console.WriteLine("Cancelled by a failed task.");
// find the failed task in tasks or via capturedException
}
if (capturedException != null && capturedException.SourceException != null)
{
Console.WriteLine("Source exception: " + capturedException.SourceException.ToString());
// could rethrow the original exception:
// capturedException.Throw();
}
}
Console.WriteLine("Results:");
tasks.ForEach((task) =>
Console.WriteLine(String.Format("Status: {0}, result: {1}",
task.Status.ToString(),
task.Status == TaskStatus.RanToCompletion? task.Result.ToString(): String.Empty)));
}
}
static void Main(string[] args)
{
var cts = new CancellationTokenSource(10000);
new Worker().DoWorkAsync(cts.Token).Wait();
Console.WriteLine("Done.");
Console.ReadLine();
}
}
}