Пример #1
0
        public IAsyncResult BeginPublicApi(AsyncCallback callback, object asyncState)
        {
            _asyncResult = new CustomAsyncResult(asyncState);
            _userCallback = callback;
            _mat.BeginA(CallbackFromA, null);

            return _asyncResult;
        }
Пример #2
0
        public IAsyncResult BeginPublicApi(AsyncCallback callback, object asyncState)
        {
            _asyncResult = new CustomAsyncResult(asyncState);
            _userCallback = callback;
            _mat.BeginA((iar) =>
                {
                    int aResultValue = _mat.EndA(iar);
                    ManualResetEvent bResultObtained = new ManualResetEvent(false);
                    _mat.BeginB(aResultValue, (iarB) =>
                        {
                            _bResultValue = _mat.EndB(iarB);
                            ProcessResultsFromBAndC();
                        }, null);
                    _mat.BeginC(aResultValue,(iarC) =>
                        {
                            _cResultValue = _mat.EndC(iarC);
                            ProcessResultsFromBAndC();
                        },null);
                }, null);

            return _asyncResult;
        }
Пример #3
0
        public IAsyncResult BeginPublicApi(AsyncCallback callback, object asyncState)
        {
            // TODO: figure out if there's a way I can return a task instead of a CustomAsyncResult, which
            //   would be more in keeping with the theme of this implementation, and show something
            //   different from the AnonymousDelegates and CallbackSeparation implementations.
            //   set a task that is the parent of all of the individual tasks?
            //   use TaskCompletionSource?

            CustomAsyncResult car = new CustomAsyncResult(asyncState);
            _taskA = _matAdapter.AAsync();
            _taskA.ContinueWith((taskA) =>
            {
                Task<int> _taskB = _matAdapter.BAsync(_taskA.Result);
                Task<int> _taskC = _matAdapter.CAsync(_taskA.Result);
                Task.WhenAll(_taskB,_taskC).ContinueWith((aggregateTask) =>
                    {
                        if (_taskB.Result > _taskC.Result)
                        {
                            Task<int> _taskD = _matAdapter.DAsync(_taskB.Result, _taskC.Result);
                            _taskD.ContinueWith((taskD) =>
                                {
                                    _finalResult = taskD.Result;
                                    car.Complete();
                                    if (callback != null) callback(car);
                                });
                        }
                        else
                        {
                            _finalResult = _taskC.Result;
                            car.Complete();
                            if (callback != null) callback(car);
                        }
                    });
            });
            // I don't have to call Start() on the tasks, it automatically calls Begin when you create the task with FromAsync.
            return car;
        }