private async Task ExecuteJobAsync(string processID, string jobID, MethodInfo methodInfo, string parameters, object instance, CancellationToken?token, bool isSync) { try { //Set job to Running if (isSync) { jobDAL.SetToRunning(processID, jobID); } else { await jobDAL.SetToRunningAsync(processID, jobID); } jobDAL.SetCachedProgressStatusAsync(jobID, JobStatus.Running); var progress = isSync ? UpdateProgressEventAsync(jobID, true).GetAwaiter().GetResult() : await UpdateProgressEventAsync(jobID, false); //Need this to update the progress of the job's //Invoke Method if (token == null) { var tokenSource = new CancellationTokenSource(); //not doing anything when using thread.Start() token = tokenSource.Token; } var args = DALHelpers.DeserializeArguments(token.Value, progress, methodInfo, parameters); methodInfo.Invoke(instance, args); } catch (TargetInvocationException exc) { if (exc.InnerException is OperationCanceledException) { if (isSync) { SetToStoppedAsync(new List <string> { jobID }, isSync).GetAwaiter().GetResult(); } else { await SetToStoppedAsync(new List <string> { jobID }, isSync); } throw exc.InnerException; } else { var job = isSync ? jobDAL.GetJob(jobID) : await jobDAL.GetJobAsync(jobID); if (job != null) { var error = job.Error + " " + exc.ToString(); var count = isSync ? SetErrorAsync(processID, job.JobID, error, isSync).GetAwaiter().GetResult() : await SetErrorAsync(processID, job.JobID, error, isSync); } throw exc; } } catch (Exception exc) { var job = isSync ? jobDAL.GetJob(jobID) : await jobDAL.GetJobAsync(jobID); if (job != null) { var error = job.Error + " " + exc.ToString(); var count = isSync ? SetErrorAsync(processID, job.JobID, error, isSync).GetAwaiter().GetResult() : await SetErrorAsync(processID, job.JobID, error, isSync); } throw exc; } //Completed successfully with no error if (isSync) { jobDAL.SetCompleted(processID, jobID); } else { await jobDAL.SetCompletedAsync(processID, jobID); } jobDAL.SetCachedProgressStatusAsync(jobID, JobStatus.Completed); }
private async Task ExecuteJobAsync(string processID, string jobID, MethodInfo methodInfo, string parameters, object instance, CancellationToken?cancelToken, PauseToken?pauseToken, bool isSync) { try { //Set job to Running if (isSync) { jobDAL.SetToRunning(processID, jobID); } else { await jobDAL.SetToRunningAsync(processID, jobID); } var progress = isSync ? UpdateProgressEventAsync(jobID, true).GetAwaiter().GetResult() : await UpdateProgressEventAsync(jobID, false); //Need this to update the progress of the job's //Invoke Method if (cancelToken == null) { var cancelSource = new CancellationTokenSource(); cancelToken = cancelSource.Token; } if (pauseToken == null) { var pauseSource = new PauseTokenSource(); pauseToken = pauseSource.Token; } var arguments = DALHelpers.DeserializeArguments(cancelToken.Value, pauseToken.Value, progress, methodInfo, parameters); var result = methodInfo.Invoke(instance, arguments); //handle async method invocation var task = result as Task; if (task != null) { if (isSync) { task.GetAwaiter().GetResult(); } else { await task; } } } catch (OperationCanceledException exc) { if (isSync) { SetToStoppedAsync(new List <string> { jobID }, isSync).GetAwaiter().GetResult(); } else { await SetToStoppedAsync(new List <string> { jobID }, isSync); } throw exc; } catch (TargetInvocationException exc) { if (exc.InnerException is OperationCanceledException) { if (isSync) { SetToStoppedAsync(new List <string> { jobID }, isSync).GetAwaiter().GetResult(); } else { await SetToStoppedAsync(new List <string> { jobID }, isSync); } throw exc.InnerException; } else { var job = isSync ? jobDAL.GetJob(jobID) : await jobDAL.GetJobAsync(jobID); if (job != null) { var error = job.Error + " " + exc.ToString(); var count = isSync ? SetErrorAsync(processID, job.JobID, error, isSync).GetAwaiter().GetResult() : await SetErrorAsync(processID, job.JobID, error, isSync); } throw exc; } } catch (Exception exc) { var job = isSync ? jobDAL.GetJob(jobID) : await jobDAL.GetJobAsync(jobID); if (job != null) { var error = job.Error + " " + exc.ToString(); var count = isSync ? SetErrorAsync(processID, job.JobID, error, isSync).GetAwaiter().GetResult() : await SetErrorAsync(processID, job.JobID, error, isSync); } throw exc; } //Completed successfully with no error if (isSync) { jobDAL.SetToCompleted(processID, jobID); } else { await jobDAL.SetToCompletedAsync(processID, jobID); } }