delegate void Mydelegate();AsyncCallback callback=new AsyncCallback(test1);
Mydelegate  mydelegate=new Mydelegate(test);
mydelegate.begininvoke(callback,null);public static  void test1(IAsyncResult ar)

//do something 
}private void test()
{
//do something 
}我想问下究竟是callback委托中关联的方法test1是回调函数,还是mydelegate委托中关联的方法是回调方法?
还有就是在test1方法中能做些什么事呢?

解决方案 »

  1.   

    test1是回调函数,你看其定义,static,一般来说回调函数都是static的。另外委托说到底就是函数指针。
      

  2.   

    test1是回调函数,invoke里你可以自己传一个state,然后回调函数通过类型转换IAsyncResult做处理
      

  3.   

    BeginInvoke是异步调用同步方法
    Asynchronous delegates allow you to call a synchronous method in an asynchronous manner. When you call a delegate synchronously, the Invoke method calls the target method directly on the current thread. If the compiler supports asynchronous delegates, it will generate the Invoke method and the BeginInvoke and EndInvoke methods. If the BeginInvoke method is called, the common language runtime (CLR) will queue the request and return immediately to the caller. The target method will be called on a thread from the thread pool. The original thread, which submitted the request, is free to continue executing in parallel with the target method, which is running on a thread pool thread. If a callback method has been specified in the call to the BeginInvoke method, the callback method is called when the target method returns. In the callback method, the EndInvokemethod obtains the return value and any in/out parameters. If no callback method is specified when calling BeginInvoke, EndInvoke can be called from the thread that called BeginInvoke.你的问题其实包含了两个delegate,一个是你定义的Mydelegate    一个是framework定义的
    public delegate void AsyncCallback(IAsyncResult ar)test是你自己Mydelegate实例mydelegate的回调方法
    test1是AsyncCallback实例callback的回调方法还有就是在test1方法中能做些什么事呢? 
    见红色部分
    也可以看下面的例子:/*
    The following example demonstrates using asynchronous methods to
    get Domain Name System information for the specified host computers.
    This example uses a delegate to obtain the results of each asynchronous 
    operation.
    */using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Collections.Specialized;
    using System.Collections;namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class UseDelegateForAsyncCallback
        {
            static int requestCounter;
            static ArrayList hostData = new ArrayList();
            static StringCollection hostNames = new StringCollection();
            static void UpdateUserInterface()
            {
                // Print a message to indicate that the application
                // is still working on the remaining requests.
                Console.WriteLine("{0} requests remaining.", requestCounter);
            }
            public static void Main()
            {
                // Create the delegate that will process the results of the 
                // asynchronous request.
                AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation);
                string host;
                do
                {
                    Console.Write(" Enter the name of a host computer or <enter> to finish: ");
                    host = Console.ReadLine();
                    if (host.Length > 0)
                    {
                        // Increment the request counter in a thread safe manner.
                        Interlocked.Increment(ref requestCounter);
                        // Start the asynchronous request for DNS information.
                        Dns.BeginGetHostEntry(host, callBack, host);
                     }
                } while (host.Length > 0);
                // The user has entered all of the host names for lookup.
                // Now wait until the threads complete.
                while (requestCounter > 0)
                {
                    UpdateUserInterface();
                }
                // Display the results.
                for (int i = 0; i< hostNames.Count; i++)
                {
                    object data = hostData [i];
                    string message = data as string;
                    // A SocketException was thrown.
                    if (message != null)
                    {
                        Console.WriteLine("Request for {0} returned message: {1}", 
                            hostNames[i], message);
                        continue;
                    }
                    // Get the results.
                    IPHostEntry h = (IPHostEntry) data;
                    string[] aliases = h.Aliases;
                    IPAddress[] addresses = h.AddressList;
                    if (aliases.Length > 0)
                    {
                        Console.WriteLine("Aliases for {0}", hostNames[i]);
                        for (int j = 0; j < aliases.Length; j++)
                        {
                            Console.WriteLine("{0}", aliases[j]);
                        }
                    }
                    if (addresses.Length > 0)
                    {
                        Console.WriteLine("Addresses for {0}", hostNames[i]);
                        for (int k = 0; k < addresses.Length; k++)
                        {
                            Console.WriteLine("{0}",addresses[k].ToString());
                        }
                    }
                }
           }        // The following method is called when each asynchronous operation completes.
            static void ProcessDnsInformation(IAsyncResult result)
            {
                string hostName = (string) result.AsyncState;
                hostNames.Add(hostName);
                try 
                {
                    // Get the results.
                    IPHostEntry host = Dns.EndGetHostEntry(result);
                    hostData.Add(host);
                }
                // Store the exception message.
                catch (SocketException e)
                {
                    hostData.Add(e.Message);
                }
                finally 
                {
                    // Decrement the request counter in a thread-safe manner.
                    Interlocked.Decrement(ref requestCounter);
                }
            }
        }
    }