我想有一个库,里面有一个函数,它接受一个对象作为它的参数。
有了这个对象,当X完成时,我希望能够调用指定的函数。将被调用的函数将由调用者指定,X将由库完成和监视。
我该怎么做呢?
作为参考,我使用C#和.NET 3.5
发布于 2015-11-05 05:45:39
您可以使用C#.NET中提供的System.Action来实现回调函数。请查看此示例:
//Say you are calling some FUNC1 that has the tight while loop and you need to
//get updates on what percentage the updates have been done.
private void ExecuteUpdates()
{
Func1(Info => { lblUpdInfo.Text = Info; });
}
//Now Func1 would keep calling back the Action specified in the argument
//This System.Action can be returned for any type by passing the Type as the template.
//This example is returning string.
private void Func1(System.Action<string> UpdateInfo)
{
int nCount = 0;
while (nCount < 100)
{
nCount++;
if (UpdateInfo != null) UpdateInfo("Counter: " + nCount.ToString());
//System.Threading.Thread.Sleep(1000);
}
}https://stackoverflow.com/questions/667742
复制相似问题