Example #1
0
 private void onTaskStateChanged(OperationContext context)
 {
     if (TaskStateChanged != null)
     {
         TaskStateChanged(this, new TaskStateChangedEventArgs(context));
     }
 }
Example #2
0
 public void AddTask(Task task, OperationContext context, Action <OperationContext> statusChangeOperation)
 {
     lock (syncObject)
     {
         tasksData.Add(task.Id, new TaskData(task, context, statusChangeOperation));
     }
     onTaskListChanged();
 }
Example #3
0
 private void setExceptionState(OperationContext arg, Action <OperationContext> operationStateCallback, Exception ex)
 {
     arg.ReturnedException = ex;
     onTaskStateChanged(arg);
     if (operationStateCallback != null)
     {
         SynchronizationContext.Send(state => operationStateCallback(arg), arg);
     }
 }
Example #4
0
 void sendThreadSafe(Action <OperationContext> method, OperationContext context)
 {
     //if (parentForm.Created)
     //{
     //    return;
     //}
     //if (parentForm.Created && parentForm.InvokeRequired)
     //{
     //    //parentForm.Invoke(new Action<Action<OperationContext>, OperationContext>(sendThreadSafe),method, context);
     //    parentForm.Invoke(method,  context);
     //}
     //else
     //{
     //    method(context);
     //}
     SynchronizationContext.Send(delegate(object arg)
     {
         method((OperationContext)arg);
     }, context);
 }
Example #5
0
 public TaskData(Task task, OperationContext context, Action <OperationContext> statusChangeOperation)
 {
     Task    = task;
     Context = context;
     StatusChangeOperation = statusChangeOperation;
 }
Example #6
0
 public Task LogUnhandledExceptions(Task task, Action <OperationContext> exceptionCallback, OperationContext operationContext)
 {
     task.ContinueWith(delegate(Task t)
     {
         ControlHelper.EnsureThreadLocalized();
         if (exceptionCallback != null)
         {
             SynchronizationContext.Send(delegate(object state)
             {
                 exceptionCallback((OperationContext)state);
             }, operationContext);
         }
         else
         {
             SynchronizationContext.Send(delegate(object state)
             {
                 throw ((Task)state).Exception;
             }, operationContext);
         }
     }, TaskContinuationOptions.OnlyOnFaulted);
     return(task);
 }
Example #7
0
 public TaskStateChangedEventArgs(OperationContext context)
 {
     this.context = context;
 }
Example #8
0
        public CancellationTokenSource RunAsynchronousOperation(Action <OperationContext> method, Action <OperationContext> operationStateCallback = null, Action <OperationContext> exceptionCallback = null, bool addToList = true)
        {
            OperationContext context = new OperationContext(this);
            var tokenSource          = new CancellationTokenSource();

            context.CancellatioToken = tokenSource.Token;

            var task = context.CurrentTask = Task.Factory.StartNew(delegate(object cancellationToken)
            {
                ControlHelper.EnsureThreadLocalized();
                var arg   = (OperationContext)cancellationToken;
                arg.State = OperationState.Started;
                onTaskStateChanged(arg);
                if (operationStateCallback != null)
                {
                    // SynchronizationContext.Send(state => operationStateCallback(arg), arg);
                    sendThreadSafe(operationStateCallback, arg);
                }
                try
                {
                    method((OperationContext)cancellationToken);
                    if (addToList && !ContainsTask(context.CurrentTask))
                    {
                        return;
                    }
                    arg.State = OperationState.Ended;
                    onTaskStateChanged(arg);

                    if (operationStateCallback != null)
                    {
                        //SynchronizationContext.Send(state => operationStateCallback(arg), arg);
                        sendThreadSafe(operationStateCallback, arg);
                    }
                }
                catch (OperationCanceledException)
                {
                    arg.State = OperationState.Cancelled;
                    onTaskStateChanged(arg);
                    if (operationStateCallback != null)
                    {
                        sendThreadSafe(operationStateCallback, arg);
                    }
                }
                catch (ProfileDeletedException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorCurrentProfileDeleted, ErrorWindow.MessageBox), arg);
                }
                catch (UserDeletedException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorProfileDeleted, ErrorWindow.MessageBox), arg);
                }
                catch (OldDataException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorOldDataModification, ErrorWindow.MessageBox), arg);
                }
                catch (ValidationException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => FMMessageBox.ShowValidationError(ex.Results), arg);
                }
                catch (EndpointNotFoundException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorConnectionProblem, ErrorWindow.MessageBox), arg);
                }
                catch (TimeoutException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorConnectionProblem, ErrorWindow.MessageBox), arg);
                }
                catch (DatabaseVersionException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorOldVersionOfBodyArchitect, ErrorWindow.MessageBox), arg);
                }
                catch (MaintenanceException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ApplicationStrings.ErrorMaintenanceMode, ErrorWindow.MessageBox), arg);
                }
                catch (Exception ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, ex.Message, ErrorWindow.EMailReport), arg);
                    //Application.Exit();
                }
                finally
                {
                    RemoveTask(arg.CurrentTask);
                }
            }, context, tokenSource.Token);

            if (addToList)
            {
                AddTask(context.CurrentTask, context, operationStateCallback);
            }
            if (exceptionCallback != null)
            {
                LogUnhandledExceptions(task, exceptionCallback, context);
            }
            return(tokenSource);
        }