예제 #1
0
 void sendThreadSafe(Action <OperationContext> method, OperationContext context)
 {
     SynchronizationContext.Send(delegate(object arg)
     {
         method((OperationContext)arg);
     }, context);
 }
예제 #2
0
 private void onTaskStateChanged(OperationContext context)
 {
     if (TaskStateChanged != null)
     {
         TaskStateChanged(this, new TaskStateChangedEventArgs(context));
     }
 }
예제 #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);
     }
 }
예제 #4
0
        private void AsyncOperationStateChange(OperationContext state)
        {
            bool startLoginOperation = state.State == OperationState.Started;

            txtUserName.IsEnabled       = !startLoginOperation;
            txtPassword.IsEnabled       = !startLoginOperation;
            btnRegister.IsEnabled       = !startLoginOperation;
            btnForgotPassword.IsEnabled = !startLoginOperation;
            btnLogin.IsEnabled          = !startLoginOperation;
            progressIndicator.IsRunning = startLoginOperation;
        }
예제 #5
0
 public void AddTask(OperationContext context, Action <OperationContext> statusChangeOperation)
 {
     tasksData.TryAdd(Task.CurrentId.Value, new TaskData(context.Task, context, statusChangeOperation));
     onTaskListChanged();
 }
예제 #6
0
 public TaskData(Task task, OperationContext context, Action <OperationContext> statusChangeOperation)
 {
     Task    = task;
     Context = context;
     StatusChangeOperation = statusChangeOperation;
 }
예제 #7
0
 public Task LogUnhandledExceptions(Task task, Action <OperationContext> exceptionCallback, OperationContext operationContext)
 {
     task.ContinueWith(delegate(Task t)
     {
         Helper.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);
 }
예제 #8
0
 public TaskStateChangedEventArgs(OperationContext context)
 {
     this.context = context;
 }
예제 #9
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;
            context.Task             = Task.Factory.StartNew(delegate(object cancellationToken)
            {
                var arg = (OperationContext)cancellationToken;
                try
                {
                    if (addToList)
                    {
                        AddTask(context, operationStateCallback);
                    }

                    Helper.EnsureThreadLocalized();

                    arg.State = OperationState.Started;
                    onTaskStateChanged(arg);
                    if (operationStateCallback != null)
                    {
                        sendThreadSafe(operationStateCallback, arg);
                    }

                    method((OperationContext)cancellationToken);
                    if (addToList && !ContainsTask(arg.Task))
                    {
                        return;
                    }
                    arg.State = OperationState.Ended;
                    onTaskStateChanged(arg);

                    if (operationStateCallback != null)
                    {
                        sendThreadSafe(operationStateCallback, arg);
                    }
                }
                catch (OperationCanceledException)
                {
                    arg.State = OperationState.Cancelled;
                    onTaskStateChanged(arg);
                    if (operationStateCallback != null)
                    {
                        sendThreadSafe(operationStateCallback, arg);
                    }
                }
                catch (LicenceException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorLicence, ErrorWindow.MessageBox), arg);
                }
                catch (ProfileDeletedException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Current.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorCurrentProfileDeleted, ErrorWindow.MessageBox), arg);
                }
                catch (UserDeletedException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorProfileDeleted, ErrorWindow.MessageBox), arg);
                }
                catch (OldDataException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorOldDataModification, ErrorWindow.MessageBox), arg);
                }
                catch (ValidationException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorCurrentProfileDeleted, ErrorWindow.MessageBox), arg);
                }
                catch (EndpointNotFoundException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Current.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorConnectionProblem, ErrorWindow.MessageBox), arg);
                }
                catch (TimeoutException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Current.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorConnectionProblem, ErrorWindow.MessageBox), arg);
                }
                catch (DatabaseVersionException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Current.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.ErrorOldVersionOfBodyArchitect, ErrorWindow.MessageBox), arg);
                }
                catch (MaintenanceException ex)
                {
                    setExceptionState(arg, operationStateCallback, ex);
                    UserContext.Current.Logout(resetAutologon: false, skipLogoutOnServer: true);
                    SynchronizationContext.Send(state => ExceptionHandler.Default.Process(ex, Strings.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(Task.CurrentId.Value);
                }
            }, context, tokenSource.Token);


            if (exceptionCallback != null)
            {
                LogUnhandledExceptions(context.Task, exceptionCallback, context);
            }
            return(tokenSource);
        }