示例#1
0
        public IAsyncResult BeginConnect(string host, int port, AsyncCallback callback, object state)
        {
            if (_BaseStream != null)
            {
                throw new InvalidOperationException();
            }

            AsyncResult ar1 = new AsyncResult(callback, state);

            ThreadPool.QueueUserWorkItem((object ar2) => {
                AsyncResult ar3 = (AsyncResult)ar2;
                try {
                    Connect(host, port);
                } catch (Exception ex) {
                    ar3.Error = ex;
                }
                ar3.IsCompleted = true;
                ((ManualResetEvent)ar3.AsyncWaitHandle).Set();
                if (ar3.Callback != null)
                {
                    ar3.Callback(ar3);
                }
            }, ar1);

            return(ar1);
        }
示例#2
0
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            AsyncResult ar1 = new AsyncResult(callback, state);

            ThreadPool.QueueUserWorkItem((object ar2) => {
                AsyncResult ar3 = (AsyncResult)ar2;
                try {
                    Write(buffer, offset, count);
                } catch (Exception ex) {
                    ar3.Error = ex;
                }
                ar3.IsCompleted = true;
                ((ManualResetEvent)ar3.AsyncWaitHandle).Set();
                if (ar3.Callback != null)
                {
                    ar3.Callback(ar3);
                }
            }, ar1);
            return(ar1);
        }