public BEncodedDictionary EndCreate(IAsyncResult result)
        {
            Check.Result(result);

            if (result != this.asyncResult)
            {
                throw new ArgumentException("The supplied async result does not correspond to currently active async result");
            }

            try {
                if (!result.IsCompleted)
                {
                    result.AsyncWaitHandle.WaitOne();
                }

                if (this.asyncResult.SavedException != null)
                {
                    throw this.asyncResult.SavedException;
                }

                return(this.asyncResult.Aborted ? null : this.asyncResult.Dictionary);
            } finally {
                this.asyncResult = null;
            }
        }
        public void AbortCreation()
        {
            TorrentCreatorAsyncResult r = asyncResult;

            if (r != null)
            {
                r.Aborted = true;
            }
        }
        IAsyncResult BeginCreate(MainLoopJob task, AsyncCallback callback, object asyncState)
        {
            if (asyncResult != null)
            {
                throw new InvalidOperationException("Two asynchronous operations cannot be executed simultaenously");
            }

            asyncResult = new TorrentCreatorAsyncResult(callback, asyncState);
            ThreadPool.QueueUserWorkItem(delegate {
                try {
                    asyncResult.Dictionary = (BEncodedDictionary)task();
                } catch (Exception ex) {
                    asyncResult.SavedException = ex;
                }
                asyncResult.Complete();
            });
            return(asyncResult);
        }