Ejemplo n.º 1
0
 void sendThreadSafe(Action <OperationContext> method, OperationContext context)
 {
     SynchronizationContext.Send(delegate(object arg)
     {
         method((OperationContext)arg);
     }, context);
 }
Ejemplo n.º 2
0
 private void onTaskStateChanged(OperationContext context)
 {
     if (TaskStateChanged != null)
     {
         TaskStateChanged(this, new TaskStateChangedEventArgs(context));
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 5
0
 public void AddTask(OperationContext context, Action <OperationContext> statusChangeOperation)
 {
     tasksData.TryAdd(Task.CurrentId.Value, new TaskData(context.Task, context, statusChangeOperation));
     onTaskListChanged();
 }
Ejemplo n.º 6
0
 public TaskData(Task task, OperationContext context, Action <OperationContext> statusChangeOperation)
 {
     Task    = task;
     Context = context;
     StatusChangeOperation = statusChangeOperation;
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 8
0
 public TaskStateChangedEventArgs(OperationContext context)
 {
     this.context = context;
 }
Ejemplo n.º 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);
        }