Exemple #1
0
        static void Main8(string[] args)
        {
            DelAdd objDelAdd = Class2.Add;        // step 2

            Console.WriteLine(objDelAdd(10, 20)); // step 3
            Console.ReadLine();
        }
Exemple #2
0
        static void Main9(string[] args)
        {
            Class2 c2        = new Class2();
            DelAdd objDelAdd = c2.Add;            // step 2

            Console.WriteLine(objDelAdd(10, 20)); // step 3
            Console.ReadLine();
        }
Exemple #3
0
        static void Main7(string[] args)
        {
            //DelAdd objDelAdd = new DelAdd(Add);
            DelAdd objDelAdd = Add;

            objDelAdd = Substract;
            Console.WriteLine(objDelAdd(10, 20));
            Console.ReadLine();
        }
Exemple #4
0
        static void Main6()
        {
            //DelAdd objDelAdd = new DelAdd(Add);
            DelAdd objDelAdd = Add;

            Console.WriteLine(objDelAdd(10, 20));
            // TO DO : Try multicast delegates with parameters and a return value

            Console.ReadLine();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            //3.实例化一个委托
            DelAdd d1     = new DelAdd(Add);
            int    result = d1(1, 2);

            Console.WriteLine(result);

            Console.ReadKey();
        }
Exemple #6
0
        static void Main(string[] args)
        {
            //2. 这个委托指向 lambda表达式
            DelAdd add1 = (x, y) => { return(x + y); };

            int restult = add1(1, 2);

            Console.WriteLine(restult);
            Console.ReadKey();
        }
Exemple #7
0
 public void FAdd(int N, DelAdd _Ad)
 {
     for (i = 0; i < N; i++)
     {
         for (j = 0; j < N; j++)
         {
             _Ad();
         }
     }
 }
Exemple #8
0
        static void huidao(IAsyncResult asc)
        {
            Console.WriteLine("回调结果--- ");

            var ascResult = (AsyncResult)asc;

            Console.WriteLine(asc.IsCompleted.ToString());//状态
            DelAdd delAdd = (DelAdd)ascResult.AsyncDelegate;

            int result = delAdd.EndInvoke(asc);

            Console.WriteLine("结果 " + result);
        }
Exemple #9
0
        static void Main7()
        {
            //DelAdd objDelAdd = new DelAdd(Add);
            //DelAdd objDelAdd = Class2.Add;   //if static
            Class2 objCls2   = new Class2();
            DelAdd objDelAdd = objCls2.Add;   //if not static


            Console.WriteLine(objDelAdd(10, 20));
            // TO DO : Try multicast delegates with parameters and a return value

            Console.ReadLine();
        }
Exemple #10
0
        static void Main(string[] args)
        {
            SubThread st = new SubThread();
            //1、用类的字段方法返回变量
            //Thread t = new Thread(new ParameterizedThreadStart(st.Add));
            MyStruct s = new MyStruct();

            s.a = 1;
            s.b = 2;

            //t.Start(s);
            //t.IsBackground = true;
            //Thread.Sleep(200);
            //Console.WriteLine(st.Result.ToString());

            //2、用委托异步调用,还是会阻塞主线程
            // 与 BeginInvoke 对应 有个 EndInvoke 方法,而且运行完毕返回 IAsyncResult 类型的返回值.
            //这样我们可以这样收集 线程函数的 返回值

            //MyDelegate dele = new MyDelegate (MyFunction);
            //IAsyncResult ref = dele.BeginInvoke(10,"abcd");
            //...
            //int result = dele.EndInvoke(ref); <----收集 返回值
            //int MyFunction(int count, string str); <----带参数和返回值的 线程函数

            //DelAdd del = new DelAdd(st.AddWithReturn);
            //IAsyncResult asc = del.BeginInvoke(s,null,null);
            //int result = del.EndInvoke(asc);
            //Console.WriteLine(result.ToString());
            //Console.WriteLine("main thread");

            //3、异步回调,不阻塞主线程
            DelAdd       del = new DelAdd(st.AddWithReturn);
            IAsyncResult asc = del.BeginInvoke(s, new AsyncCallback(huidao), null);

            //int result = del.EndInvoke(asc);
            //Console.WriteLine(result.ToString());
            Console.WriteLine("main thread---");
            Console.WriteLine("main thread---");
            Console.WriteLine("main thread---");



            Console.ReadKey();
        }
Exemple #11
0
        static void Main()
        {
            HttpChannel ch = new HttpChannel();
            ChannelServices.RegisterChannel(ch,false);
            INumber robj = (INumber)Activator.GetObject(
                typeof(INumber),
                "http://localhost:1234/RemoteNumber.soap");
            Console.WriteLine("Client.Main(): Referencia para objecto remoto adquirida");

            DateTime start = System.DateTime.Now;
            Console.WriteLine("Client.Main(): Vai colocar novo valor=42");

            // Invocação Assíncrona do método setValue()
            DelSetValue svdel = new DelSetValue(robj.setValue);
            IAsyncResult svasyncres = svdel.BeginInvoke(42, null, null);
            // Invocação Assíncrona do método add()
            DelAdd adddel = new DelAdd(robj.add);
            IAsyncResult addsyncres = adddel.BeginInvoke(42,100,null, null);

            // Continua a realizar Acções

            // Obter os resultados da invocação assíncrona
            Console.WriteLine("Client.Main(): Obter resultado do SetValue()");
            svdel.EndInvoke(svasyncres);
            Console.WriteLine("Client.Main(): Obter resultado de add()");
            int soma = adddel.EndInvoke(addsyncres);
            Console.WriteLine("Client.Main(): soma: {0}", soma);

            Console.WriteLine("Client.Main(): Vai ler o valor");
            int tmp = robj.getValue();
            Console.WriteLine("Client.Main(): Novo valor no server: {0}", tmp);

            DateTime end = System.DateTime.Now;
            TimeSpan texec = end.Subtract(start);
            Console.WriteLine("Client.Main(): Tempo de Execução: {0}s:{1}ms", texec.Seconds, texec.Milliseconds);

            Console.ReadLine();
        }
Exemple #12
0
 static int PassMethodToCallAsAParameter(DelAdd objDelAdd, int a, int b)// objDelAdd = Add , a =20 , b= 10
 {
     return(objDelAdd(a, b));
 }
Exemple #13
0
        static void Main()
        {
            HttpChannel ch = new HttpChannel();
            ChannelServices.RegisterChannel(ch,false);
            INumber robj = (INumber)Activator.GetObject(
                typeof(INumber),
                "http://localhost:1234/RemoteNumber.soap");

            Console.WriteLine("Client.Main(): Vai colocar novo valor=100");
            // Invocação Assíncrona do método SetValue
            DelSetValue svdel = new DelSetValue(robj.setValue);

            AsyncCallback mycallback = new AsyncCallback(setValueCompleted);
            start = System.DateTime.Now;
            IAsyncResult svasyncres = svdel.BeginInvoke(100, mycallback, "set value com 100");
            while (!svasyncres.IsCompleted)
            {   // O cliente continua a fazer trabalho até a operação terminar
                Console.WriteLine("espera fim operação");
                Console.WriteLine("novo valor= {0}", robj.getValue());
                Thread.Sleep(1000);
            }
            Console.WriteLine("novo valor= {0}", robj.getValue());

            AsyncCallback myCbadd = new AsyncCallback(AddCompleted);
            DelAdd adddel = new DelAdd(robj.add);
            IAsyncResult svasyncresadd = adddel.BeginInvoke(100, 200, myCbadd, "add 100 com 200");

            while (!svasyncresadd.IsCompleted)
            {   // O cliente continua a fazer trabalho até a operação terminar
                Console.WriteLine("espera fim operação add");

                Thread.Sleep(1000);
            }
            Console.WriteLine("RES="+resAdd);

            Console.ReadLine();
        }
Exemple #14
0
 static int PassMethodeToCallAsParameter(DelAdd objDelAdd, int a, int b)
 {
     return(objDelAdd(a, b));
 }