Пример #1
0
        private void btn_Async_Click(object sender, EventArgs e)
        {
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++btn_Async_Click {0} 开始", Thread.CurrentThread.ManagedThreadId);


            doSomeMethodDelegate doSomeMethod = new doSomeMethodDelegate(DoSomeThing);

            IAsyncResult asyncResult = null;

            AsyncCallback asyncCallback = b => {
                Console.WriteLine(asyncResult.Equals(b));
                Console.WriteLine(b.AsyncState);
                Console.WriteLine("回调函数!");
            };

            asyncResult = doSomeMethod.BeginInvoke("btn_Async_Click", asyncCallback, "abcdefg");
            asyncResult = doSomeMethod.BeginInvoke("btn_Async_Click", xx =>
            {
                Console.WriteLine(xx.AsyncState);
                Console.WriteLine("回调函数!");
            }, "abcdefg");

            int i = 1;

            while (!asyncResult.IsCompleted)
            {
                Console.WriteLine("+++++++++++正在计算中!++++++++++ + 已经完成 {0}", 10 * i++);
                Thread.Sleep(100);
            }

            asyncResult.AsyncWaitHandle.WaitOne();
            asyncResult.AsyncWaitHandle.WaitOne(100);
            asyncResult.AsyncWaitHandle.WaitOne(1000);

            doSomeMethod.EndInvoke(asyncResult);

            Func <int, string> func = z => {
                DoSomeThing("btn_Async_Click");
                return("2017----");
            };

            asyncResult = func.BeginInvoke(DateTime.Now.Millisecond, a =>
            {
                Console.WriteLine(a.AsyncState);
                Console.WriteLine("这里是回调函数 {0}", Thread.CurrentThread.ManagedThreadId);
            }, "AlwaysOnline");

            string sresult = func.EndInvoke(asyncResult);

            //ThreadPool.QueueUserWorkItem(x => { });
            //ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            //manualResetEvent.Set();
            //manualResetEvent.Reset();
            TaskFactory taskFactory = new TaskFactory();

            taskFactory.ContinueWhenAll(new Task[3], x0 => { });
            Action <string> action  = x3 => { Console.WriteLine("{0}", x3); };
            Action <string> action1 = x1 => DoSomeThing("a");

            Func <string, int> funccc = x2 => {
                Console.WriteLine("{0}", x2);
                return(3);
            };
            int x = funccc("ab");

            Parallel.Invoke();
            Parallel.ForEach(new string[] { "a" }, (y, state) => {
                Console.Write(y);
                state.Break();
            });

            ParallelOptions parallelOptions = new ParallelOptions();

            parallelOptions.MaxDegreeOfParallelism = 5;

            //Parallel.For<string>()
            CancellationTokenSource cts = new CancellationTokenSource();

            //cts.Token;
            cts.Cancel();
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++btn_Async_Click {0} 结束", Thread.CurrentThread.ManagedThreadId);
        }
Пример #2
0
        private KeyValuePair<SocketInformation, Boolean> GainSocket(IAsyncResult Result)
        {
            if (Result.Equals(null))
            {
                return new KeyValuePair<SocketInformation, Boolean>(new SocketInformation(), false);
            }

            try
            {
                Socket Sock = base.EndAccept(Result);

                if (Sock == null)
                {
                    return new KeyValuePair<SocketInformation, Boolean>(new SocketInformation(), false);
                }

                if (BrickEngine.GetSocketShield().GetSocketDefender().Troubleshoot(Sock.RemoteEndPoint.ToString().Split(':')[0]))
                {
                    Sock.Shutdown(SocketShutdown.Both);
                    Sock.Dispose();
                    Sock.Close();

                    BrickEngine.GetProgressReactor().GetCollector().Finialize(Sock);

                    return new KeyValuePair<SocketInformation, Boolean>(new SocketInformation(), false);
                }

                return new KeyValuePair<SocketInformation, Boolean>(Sock.DuplicateAndClose(ProcessId), true);
            }
            catch { return new KeyValuePair<SocketInformation, Boolean>(new SocketInformation(), false); }
        }