Ejemplo n.º 1
0
        /// <summary>
        /// Copy a file async
        /// </summary>
        /// <param name="source">Stream source</param>
        /// <param name="destination">Stream destination</param>
        /// <param name="completed">Action()</param>
        public static void CopyStreamToStream(FileStream source, FileStream destination,
                                              Action <FileStream, FileStream, Exception> completed)
        {
            byte[] buffer = new byte[0x1024];
            System.ComponentModel.AsyncOperation asyncOp = System.ComponentModel.AsyncOperationManager.CreateOperation(null);

            Action <Exception> done = e =>
            {
                if (completed != null)
                {
                    asyncOp.Post(delegate
                    {
                        completed(source, destination, e);
                    }, null);
                }
            };

            AsyncCallback rc = null;

            rc = readResult =>
            {
                try
                {
                    int read = source.EndRead(readResult);
                    if (read > 0)
                    {
                        destination.BeginWrite(buffer, 0, read, writeResult =>
                        {
                            try
                            {
                                destination.EndWrite(writeResult);
                                source.BeginRead(
                                    buffer, 0, buffer.Length, rc, null);
                            }
                            catch (Exception exc) { done(exc); }
                        }, null);
                    }
                    else
                    {
                        done(null);
                    }
                }
                catch (Exception exc) { done(exc); }
            };
            source.BeginRead(buffer, 0, buffer.Length, rc, null);
        }
Ejemplo n.º 2
0
		/// <summary>
		/// 状態を初期化する
		/// </summary>
		protected virtual void Initialize()
		{

			// 既に接続中なら中止する
			if (this.IsConnected) {
				Disconnect();
			}

			_asyncOperation = System.ComponentModel.AsyncOperationManager.CreateOperation(null);

		}
Ejemplo n.º 3
0
		/// <summary>
		/// コンストラクタ
		/// </summary>
		public ChatClient()
		{
			_cancelEvent = new System.Threading.ManualResetEvent(true);
			_asyncOperation = null;
		}