Ejemplo n.º 1
0
        public static void FooCallBack(IAsyncResult ar)
        {
            PrintCurrThreadInfo("FooCallBack()");
            AsyncFoo caller = (AsyncFoo)ar.AsyncState;

            caller.EndInvoke(ar);
        }
Ejemplo n.º 2
0
    static void FooCallBack(IAsyncResult ar)
    {
        AsyncFoo caller = (AsyncFoo)ar.AsyncState;
        int      i      = caller.EndInvoke(ar);

        PrintCurrThreadInfo("FooCallBack() " + i.ToString() + "  ");
    }
        public void TestAyncFooReturnsNumberOfBytes()
        {
            IAsyncFoo sut    = new AsyncFoo();
            var       url    = "http://www.google.com/";
            var       t      = sut.CountBytesAsync(new HttpClient(), url);
            var       actual = t.Result;

            Console.WriteLine($"{url} returned {actual} bytes");
            Assert.True(actual > 1);
        }
Ejemplo n.º 4
0
 private static void PostAsync(object i)
 {
     try
     {
         AsyncFoo caller = Myfunc;
         caller.BeginInvoke(i.ToString(), FooCallBack, caller);
     }
     catch (Exception es)
     {
         throw;
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 客户端不停的接收服务端发送过来的消息
        /// </summary>
        /// <param name="o"></param>
        void GetData(object o)
        {
            if (this.InvokeRequired)
            {
                AsyncFoo dele = new AsyncFoo(GetData);
                this.Invoke(dele, o);
            }
            else
            {
                while (true)
                {
                    try
                    {
                        //将服务端发过来的数据先放到一个字节数组里面去
                        byte[] buffer = new byte[1024 * 1024 * 2]; //创建一个字节数组,字节数组的长度为2M

                        //实际接收到的有效字节数; (利用Receive方法接收客户端传过来的数据,然后把数据保存到buffer字节数组中,返回一个接收到的数据的长度)
                        int r = socketSend.Receive(buffer);

                        if (r == 0) //如果接收到的有效字节数为0 说明客户端已经关闭了。这时候就跳出循环了。
                        {
                            //只有客户端给用户发消息,不可能是发0个长度的字节。即便发空消息,空消息也是有过个长度的。所有接收到的有效字节长度为0就代表客户端已经关闭了
                            break;
                        }


                        //将buffer这个字节数组里面的数据按照UTF8的编码,解码成我们能够读懂的的string类型,因为buffer这个数组的实际存储数据的长度是r个 ,所以从索引为0的字节开始解码,总共解码r个字节长度。
                        string str = Encoding.UTF8.GetString(buffer, 0, r);


                        //RemoteEndPoint方法是获取服务器的IP地址和端口号
                        ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + str);
                    }
                    catch
                    {
                    }
                }
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            AsyncFoo foo = (UInt64 size) =>
            {
                UInt64 f = 1;
                for (UInt64 i = 2, tmp; i <= size; i++)
                {
                    try { checked { tmp = f * i; } }
                    catch (Exception)
                    {
                        Console.WriteLine("Overflow, max value returned");
                        return(f);
                    }
                    f *= i;
                }
                return(f);
            };

            UInt64 n = 21U;

            foo.BeginInvoke(n, (ar) => { Console.WriteLine(foo.EndInvoke(ar)); }, null);

            Console.ReadKey();
        }
Ejemplo n.º 7
0
    /// ﹤summary﹥
    /// 投递一个异步调用
    /// ﹤/summary﹥
    static void PostAsync(int i)
    {
        AsyncFoo caller = new AsyncFoo(Foo);

        caller.BeginInvoke(i, new AsyncCallback(FooCallBack), caller);
    }
Ejemplo n.º 8
0
        /// ﹤summary﹥
        /// 投递一个异步调用
        /// ﹤/summary﹥
        public static void PostAsync()
        {
            AsyncFoo caller = new AsyncFoo(Foo);

            caller.BeginInvoke(1000, new AsyncCallback(FooCallBack), caller);
        }