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方法中能做些什么事呢?
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方法中能做些什么事呢?
解决方案 »
- winform 在combobox中增加一行“请选择”,???
- 一个截图函数
- asp.net(C#)开发的网站中ajax传值时出现js 拒绝访问 在线等答案 可以加分!!!
- C# 线程问题
- 在LINQ查询的where语句中将字符串转换为数字
- 谁有datawindow.net的在c#使用的详细资料?
- 请问能不能将多个工程编译为一个DLL发布?
- 如何把DataSet中的数据生成一个XML文件和一个TXT文件?
- 我给RICHTEXTBOX定义了一个CONTEXTMENU,我想弹出菜单时在此菜单的鼠标是箭头,当进入RICHTEXTBOX时是‘工’字型?
- 求提供C#常用函數
- 为窗体的控件添加事件处理代码
- 请大家推荐几本好点的书
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);
}
}
}
}