开发者

How to pass Callback in c#?

开发者 https://www.devze.com 2023-03-25 06:12 出处:网络
When i call a async function from third party library then i found that the function need to pass the callback.

When i call a async function from third party library then i found that the function need to pass the callback.

the function is

Library.getResult(object options, Action<library.libresult<dataCollection>>  callback

can someone tell me how i can pass callback to this function in s开发者_Python百科ilverlight.

thanks


You use a delegate for method that accepts the result. You can for example use a lambda expression to create that:

Library.getResult(options, result => {
  // code here runs when the result arrives
});

You can also declare a named method:

private void HandleResult(library.libresult<dataCollection> result) {
  //...
}

Then just use its name in the call, which will automatically create a delegate for it:

Library.getResult(options, HandleResult);


An Action<library.libresult<dataCollection>> is a method that takes a library.libresult<dataCollection>, so the following should work:

Library.getResult(options, result => {
     // do something with result
});

Here, result will be the library.libresult<dataCollection> it gives you back. Presumably (but not for certain) this is async, so you may need to consider the threading issues etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消