Ejemplo n.º 1
0
        private void buttonUsingADelegate_Click(object sender, EventArgs e)
        {
            Console.WriteLine("\n**** Using A Delegate Demo ****\n");
            //Like C++ you can use a delegate to point to a Static method...
            MethodPointer1 objMP_A = new MethodPointer1(MyMethodPointerDemoClass.StaticMethod);
            Console.WriteLine(objMP_A.Invoke("Test1\n"));

            //UN-Like C++ you can also use a delegate to point to a Instance method...
            MyMethodPointerDemoClass objC1 = new MyMethodPointerDemoClass();
            MethodPointer1 objMP_B = new MethodPointer1(objC1.InstanceMethod);
            Console.WriteLine(objMP_B.Invoke("Test2\n"));

            //You can also "Invoke" a delegate by simply using it like a method call...
            Console.WriteLine(objMP_B("Test3\n"));


            //Using a Delegate, really creates an Invisible class with useful Meta-Data. 
            //the new class inherits from the base class called System.MulticastDelegate 
            System.Reflection.MethodInfo objMI = objMP_B.Method;
            Console.WriteLine("\n**** Meta-Data ****\n");
            Console.WriteLine("\tMethod Name: {0} ", objMI.Name);
            Console.WriteLine("\tMethod ReturnType: {0} ", objMI.ReturnType.ToString());
            Console.WriteLine("\tMethod Attributes: {0} ", objMI.Attributes.ToString());
            Console.WriteLine("\tMethod IsConstructor: {0} ", objMI.IsConstructor.ToString());
        }
Ejemplo n.º 2
0
        private void buttonAsyncPolling_Click(object sender, EventArgs e)
        {
            Console.WriteLine("\n\n**** Using A Async Delegate call with \"Polling\" ****");
            objMP_Async = MyMethodPointerDemoClass.LongRunningMethod;
            //Note: The (AsyncCallback) argument and the (System.Object) argument are not used here either.
            IAsyncResult objAR = objMP_Async.BeginInvoke("Polling Test", null, null);

            while (!objAR.IsCompleted)
            {
                Console.WriteLine("Doing other stuff until the other thread finishes");
                System.Threading.Thread.Sleep(1000);
            }

            //Now, continue on...
            Console.WriteLine("Finally! Continuing back on Main thread");
            //Get the Async Results...
            Console.WriteLine("\n\t*Here is the returned data: " + objMP_Async.EndInvoke(objAR));

        }
Ejemplo n.º 3
0
 private void buttonAsyncFireAndForget_Click(object sender, EventArgs e)
 {
     Console.WriteLine("\n\n**** Using A Async Delegate call with \"Fire and Forget\" ****");
     objMP_Async = MyMethodPointerDemoClass.LongRunningMethod;
     //Note: If your method has parameters, you supply them at the beginning of the argument list.
     //The 2nd to the last argument (AsyncCallback) and last argument (System.Object) follow these,
     //but are not needed here. So, we just put null in their place. 
     objMP_Async.BeginInvoke("Fire and Forget Test", null, null);
     Console.WriteLine("Continuing on Main thread");
     //We don't care about the Async Results since we are demonstrating fire and forget!
 }
Ejemplo n.º 4
0
 private void buttonCompletionNotification_Click(object sender, EventArgs e)
 {
     Console.WriteLine("\n\n**** Using A Async Delegate call with \"Completion Notification\" ****");
     objMP_Async = MyMethodPointerDemoClass.LongRunningMethod;
     //Note: This time the (AsyncCallback) argument and the (System.Object) argument ARE Used  
     //EXAMPLE:  BeginInvoke("Parameter Data", 
     //                      (AsyncCallback) = new AsyncCallback(Method to call on completion), 
     //                      (System.Object) = Delegate to describe the original Method being called);
     IAsyncResult objAR = objMP_Async.BeginInvoke("Completion Notification Test"
                                                 , new AsyncCallback(CallBackMethod)
                                                 , objMP_Async);
     Console.WriteLine("Not waiting this time to return to Main thread");
 }
Ejemplo n.º 5
0
        private void buttonWaitForCompletion_Click(object sender, EventArgs e)
        {
            //Make an Async call to another thread and wait (But you can timeout if this call takes too long)

            Console.WriteLine("\n**** Using A Async Delegate call with \"Polling\" ****");
            objMP_Async = MyMethodPointerDemoClass.LongRunningMethod;
            //Note: The (AsyncCallback) argument and the (System.Object) argument are not used here  
            IAsyncResult objAR = objMP_Async.BeginInvoke("Some Text", null, null);

            if (!objAR.AsyncWaitHandle.WaitOne(3000)) //Note: You would normally wait longer then 3 seconds!
            {
                Console.WriteLine("\nGive Up! The other Thread took too long");
            }
            else
            {
                //Get the Async Results...
                Console.WriteLine("\n\t*Here is the returned data: " + objMP_Async.EndInvoke(objAR));
            }
            //Continue on...
            Console.WriteLine("Continuing back on Main thread");

        }