コード例 #1
0
        static void CallbackFunc(IAsyncResult result)
        {
            //result 是“加法类.Add()方法”的返回值
            //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging
            //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。
            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;

            Console.WriteLine(handler.EndInvoke(result));
            Console.WriteLine(result.AsyncState);
        }
コード例 #2
0
        /// <summary>
        /// 异步方法
        /// 主线程并没有等待,而是直接向下运行了。但是问题依然存在,
        /// 当主线程运行到EndInvoke时,如果这时调用没有结束(这种情况很可能出现),
        /// 这时为了等待调用结果,线程依旧会被阻塞。
        /// </summary>
        public void Asynchronous_Method()
        {
            Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            AddHandler handler = new AddHandler(Add);
            //IAsyncResult: 异步操作接口(interface)
            //BeginInvoke: 委托(delegate)的一个异步方法的开始
            IAsyncResult result = handler.BeginInvoke(1, 2, null, null);

            Console.WriteLine("继续做别的事情。。。");
            //异步操作返回
            Console.WriteLine(handler.EndInvoke(result));
        }
コード例 #3
0
        public static void Main_S()
        {
            Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            AddHandler handler = new AddHandler(AddClass.Jian);

            //IAsyncResult: 异步操作接口(interface)
            //BeginInvoke: 委托(delegate)的一个异步方法的开始
            IAsyncResult result = handler.BeginInvoke(1, 2, 3, null, null);

            Console.WriteLine("------继续做别的事情。。。\n");

            //异步操作返回
            Console.WriteLine(handler.EndInvoke(result));
            return;
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: ChengPeng-Git/Socket_Client
        public void cbfunc(IAsyncResult result)
        {
            //result 是“加法类.Add()方法”的返回值
            //AsyncResult 是IAsyncResult接口的一个实现类,空间:System.Runtime.Remoting.Messaging
            //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。
            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
            //Console.WriteLine(handler.EndInvoke(result));
            //Console.WriteLine(result.AsyncState);
            Action ac = () =>
            {
                OutputLine("hh is " + handler.EndInvoke(result).ToString());
                OutputLine((string)result.AsyncState);
            };

            Invoke(ac);
        }
コード例 #5
0
ファイル: Async.cs プロジェクト: VTS2018/DemoThreadPools
        static void CallbackFuction(IAsyncResult result)
        {
            //result 是“加法类.Add()方法”的返回值

            //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging
            //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。

            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;

            //MethodInfo m = handler.Method;
            //Console.WriteLine(m.Name);
            //ParameterInfo[] par = m.GetParameters();

            Console.WriteLine(handler.EndInvoke(result)); //结束异步返回计算的结果
            Console.WriteLine(result.AsyncState);         //异步结束后的状态
        }
コード例 #6
0
ファイル: Async.cs プロジェクト: VTS2018/DemoThreadPools
        /*运行结果:
         * ===== 同步调用 SyncInvokeTest =====
         * 开始计算:1+2
         * 计算完成!
         * 继续做别的事情。。。
         * 3
         */
        #endregion

        #region 异步调用
        public static void AsynchronousMain()
        {
            Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            //你会发现这里的ManagedThreadId和执行 Add方法的 线程ID不一致的,我们使用异步的方式 简洁的操作了线程
            Console.WriteLine("AsynchronousMain当前线程唯一表示:" + Thread.CurrentThread.ManagedThreadId);

            AddHandler handler = new AddHandler(Additive.Add);

            //IAsyncResult: 异步操作接口(interface)
            //BeginInvoke: 委托(delegate)的一个异步方法的开始
            //BeginInvoke开始一个异步操作

            //委托人:开始BeginInvoke
            IAsyncResult result = handler.BeginInvoke(1, 2, null, null);

            Console.WriteLine("继续做别的事情。。。");


            //委托人:监控异步操作返回
            Console.WriteLine(handler.EndInvoke(result));
            Console.ReadKey();
        }
コード例 #7
0
        private void button1_Click(object sender, EventArgs e)
        {
            Thread     thread = null;
            AddHandler add    = new AddHandler((a, b) =>
            {
                thread = Thread.CurrentThread;
                System.Threading.Thread.Sleep(10000);
                return(a + b);
            });
            var ar = add.BeginInvoke(1, 2, iar => { }, add);

            if (!ar.AsyncWaitHandle.WaitOne(1000, true))
            {
                thread.Abort();
                //ar.AsyncWaitHandle.Close();
                textBox1.Text = "timeout!";
            }
            else
            {
                textBox1.Text = add.EndInvoke(ar).ToString();
                ar.AsyncWaitHandle.Close();
            }
        }
コード例 #8
0
ファイル: Form1.cs プロジェクト: rajayaseelan/mysoftsolution
        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = null;
            AddHandler add = new AddHandler((a, b) =>
            {
                thread = Thread.CurrentThread;
                System.Threading.Thread.Sleep(10000);
                return a + b;
            });
            var ar = add.BeginInvoke(1, 2, iar => { }, add);

            if (!ar.AsyncWaitHandle.WaitOne(1000, true))
            {
                thread.Abort();
                //ar.AsyncWaitHandle.Close();
                textBox1.Text = "timeout!";
            }
            else
            {
                textBox1.Text = add.EndInvoke(ar).ToString();
                ar.AsyncWaitHandle.Close();
            }
        }
コード例 #9
0
        static void Main(string[] args)
        {
            string  haha  = "1.2346";
            string  xixi  = "1.2346";
            decimal dhaha = decimal.Parse(haha);
            decimal dxixi = decimal.Parse(xixi);

            if (dhaha == dxixi)
            {
                Console.WriteLine("不得不服");
            }
            Dictionary <string, int> dir = new Dictionary <string, int>();

            dir.Add("1", 1);
            dir.Add("3", 3);
            dir.Add("2", 2);

            Dictionary <string, 加法类> PriceReceive = new Dictionary <string, 加法类>();

            PriceReceive.Add("RETUR", new 加法类());
            PriceReceive.Add("SAXO", new 加法类());
            PriceReceive.Add("SP", new 加法类());
            Dictionary <string, 加法类> PriceReceive1 = new Dictionary <string, 加法类>();

            PriceReceive1.Add("SAXO", new 加法类());
            PriceReceive1.Add("RETUR", new 加法类());
            PriceReceive1.Add("SP", new 加法类());

            //Hashtable PriceReceive2 = new Hashtable();
            //PriceReceive2 = PriceReceive;

            foreach (string item in PriceReceive.Keys)
            {
                Console.WriteLine(item);
            }
            PriceReceive = new Dictionary <string, 加法类>();
            foreach (string item in PriceReceive1.Keys)
            {
                PriceReceive.Add(item, PriceReceive1[item]);
            }

            foreach (string item in PriceReceive.Keys)
            {
                Console.WriteLine(item);
            }



            Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++");


            string t1 = string.Empty;

            if (t1 == "")
            {
                Console.WriteLine("123");
            }
            Console.WriteLine(t1.Length);
            加法类 te = new 加法类();

            te.name = "123";
            加法类 se = te;

            te.name = "345";
            Console.WriteLine(se.name);

            decimal f = 20;
            decimal t = Convert.ToDecimal(f / 100);

            Console.WriteLine(t);
            Console.WriteLine(DateTime.Now.Day);
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

            string        AddProductList  = "AUDUSD_";
            List <string> lstTradeProduct = new List <string>(AddProductList.Split('_'));

            for (int i = 0; i < lstTradeProduct.Count; i++)
            {
                Console.WriteLine(lstTradeProduct[i] + "___");
            }

            if (DateTime.Now.Hour == 15)
            {
                Console.WriteLine("你想不到吧");
            }
            string test = "/fefg123";

            Console.WriteLine(test.ToUpper());
            //Console.WriteLine("===== 同步调用 SyncInvokeTest =====");
            //AddHandler handler = new AddHandler(加法类.Add);
            //int result = handler(1, 2);
            //Console.WriteLine("继续做别的事情。。。");
            //Console.WriteLine(result);
            //Console.ReadKey();

            //Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            //AddHandler handler = new AddHandler(加法类.Add);
            ////IAsyncResult: 异步操作接口(interface)
            ////BeginInvoke: 委托(delegate)的一个异步方法的开始
            //IAsyncResult result = handler.BeginInvoke(1, 2, null, null);
            //Console.WriteLine("继续做别的事情。。。");
            ////异步操作返回
            //Console.WriteLine(handler.EndInvoke(result));
            //Console.ReadKey();

            Console.WriteLine("===== 异步回调 AsyncInvokeTest =====");
            AddHandler handler = new AddHandler(加法类.Add);
            //异步操作接口(注意BeginInvoke方法的不同!)
            IAsyncResult result = handler.BeginInvoke(1, 2, ar => handler.EndInvoke(ar), "AsycState:OK");

            Console.WriteLine("继续做别的事情。。。");
            Console.ReadKey();
        }