I have been trying to figure out how an error is occurring in my code. The exception tells me a commit is already in progress, but unless a call to SaveChanges
is asynchronous, I don't see how that is happening.
I have a Scheduler
class that holds multiple Task
objects.
Each Task
has a BackgroundWorker
that does processing in another thread. I then have an event handler for this BackgroundWorker complete event in the Task
class with the following code:
private void TaskWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!(e.Result is TaskResult))
throw new ArgumentException("Result must be a TaskResult class.");
TaskComplete((TaskResult)e.Result);
}
Still with me? So I have this event handler in my task class that fires event TaskComplete
that I handle in my main Scheduler
class with the following code:
private void TaskCompleted(object sender, TaskCompletedEvent e)
{
Model.Task scheduledTask = entitySet.Tasks.First(x => x.TaskName == e.ClassName);
TaskLog logMsg = new TaskLog()
{
//stuff here
};
scheduledTask.TaskLogs.Add(logMsg);
entitySet.SaveChanges();
}
Now at this point, to my understanding, I am back in my main thread because the work that is done in my backgroundworker has completed. When I had 5 tasks running very frequently, I was getting an exception on the SaveChanges saying a commit was already in progress. I don't understand how this could be since I am not sharing this context across thread. The only way I could see this happening is if SaveChanges is asynchronous (not a blocking call). I know wrapping the code inside of TaskCompleted with a using statement and a new context would fix it, but I want to know why. And why isn't working in its current state.
One last thing, I am using Telerik's OpenAccess ORM.