Пример #1
0
        private void InitThread(object context)
        {
            var modelLevelThContext = (System.Runtime.Remoting.Contexts.Context)context;
            bool inited = false;

            #region Infinity try to connect then init and exit

            do
            {
                try
                {
                    using (var sClient = new AccountService.AccountServiceClient())
                    {
                        var getAccountsTask = sClient.GetAllAsync();
                        var getAccountsResTask = getAccountsTask.ContinueWith(res => 
                        {
                            stopCancellationTokenSource.Token.ThrowIfCancellationRequested();
                            if (res.Result.Error != null)
                                throw new Exception(res.Result.Error);
                            return res.Result.Values;
                        }, stopCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current);

                        var getMarksTask = sClient.GetColumnMarksAsync();
                        var getMarksResTask = getMarksTask.ContinueWith(res =>
                        {
                            stopCancellationTokenSource.Token.ThrowIfCancellationRequested();
                            if (res.Result.Error != null)
                                throw new Exception(res.Result.Error);
                            return res.Result.Values;
                        }, stopCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current);

                        var getImportQueueRecordStatesTask = sClient.GetImportQueueRecordStatesAsync();
                        var getImportQueueRecordStatesResTask = getImportQueueRecordStatesTask.ContinueWith(res =>
                        {
                            stopCancellationTokenSource.Token.ThrowIfCancellationRequested();
                            if (res.Result.Error != null)
                                throw new Exception(res.Result.Error);
                            return res.Result.Values;
                        }, stopCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current);

                        var getColumnTypesTask = sClient.GetColumnTypesAsync();
                        var getColumnTypesResTask = getColumnTypesTask.ContinueWith(res =>
                        {
                            stopCancellationTokenSource.Token.ThrowIfCancellationRequested();
                            if (res.Result.Error != null)
                                throw new Exception(res.Result.Error);
                            return res.Result.Values;
                        }, stopCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current);

                        Task.WaitAll(new Task[] { getAccountsTask, getMarksTask, getColumnTypesTask, getImportQueueRecordStatesTask }, stopCancellationTokenSource.Token);
                        Task.WaitAll(new Task[] { getAccountsResTask, getMarksResTask, getColumnTypesResTask, getImportQueueRecordStatesResTask }, stopCancellationTokenSource.Token);

                        modelLevelThContext.DoCallBack(() => RaiseAccountInitialize(getAccountsResTask.Result.ToArray()));
                        modelLevelThContext.DoCallBack(() => RaiseMarkInitialize(getMarksResTask.Result.ToArray()));
                        modelLevelThContext.DoCallBack(() => RaiseColumnTypesInitialize(getColumnTypesResTask.Result.ToArray()));
                        modelLevelThContext.DoCallBack(() => RaiseImportQueueRecordStateInitialize(getImportQueueRecordStatesResTask.Result.ToArray()));

                        inited = true;
                        SetError((string)null);
                    }
                }
                catch(ThreadAbortException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    SetError(ex);
                    Thread.Sleep(ConnectionTimeInterval);
                }
            } while (!inited);

            IsLoaded = true;

            #endregion
        }