Пример #1
0
        /// <summary>
        /// Ends a pending asynchronous operation started with <see cref="BeginGetResponse"/>.
        /// </summary>
        /// <param name="asyncResult">The <see cref="IAsyncResult"/> that was returned when the operation started.</param>
        /// <returns>A <see cref="WebResponse"/> reference that contains an <see cref="FtpWebResponse"/> instance.
        /// This object contains the FTP server's response to the request.</returns>
        public override WebResponse EndGetResponse(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            FtpAsyncResult far = (FtpAsyncResult)asyncResult;

            if (far.IsCompleted)
            {
                if (response == null)
                {
                    throw new WebException(Properties.Resources.net_could_not_connect, WebExceptionStatus.ConnectFailure);
                }
                return(this.response);
            }

            //wait till operation finished
            far.AsyncWaitHandle.WaitOne();
            //mark as synchronous completion
            far.completedSynchronously = true;
            far.isCompleted            = true;

            if (response == null)
            {
                throw new WebException(Properties.Resources.net_could_not_connect, WebExceptionStatus.ConnectFailure);
            }
            return(this.response);
        }
Пример #2
0
        /// <summary>
        /// Begins sending a request and receiving a response from an FTP server asynchronously.
        /// </summary>
        /// <param name="callback">An <see cref="AsyncCallback"/> delegate that references the method to invoke when the operation is complete.</param>
        /// <param name="state">A user-defined object that contains information about the operation.
        /// This object is passed to the callback delegate when the operation completes.</param>
        /// <returns>An <see cref="IAsyncResult"/> instance that indicates the status of the operation.</returns>
        public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
        {
            if (gettingResponse)
            {
                throw new InvalidOperationException();
            }

            gettingResponse = true;
            asyncResult     = new FtpAsyncResult(this, state, callback);

            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(GetResponseThread), asyncResult);
            //System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(GetResponseThread));
            //t.IsBackground = true;
            //t.Start();
            return(asyncResult);
        }