太多的论点为的BeginXXX FromAsync? [英] Too many arguments in BeginXXX for FromAsync?
问题描述
我有一个异步方法具有以下签名:
I have an async method with the following signature:
IAsyncResult BeginGetMyNumber(string foo, string bar, string bat, int bam, AsyncCallback callback, object state)
我想用Factory.FromAsync这样执行它:
I want to execute it using Factory.FromAsync like this:
var result = Task<int>.Factory.FromAsync(
instance.BeginGetMyNumber,
instance.EndGetMyNumber,
"foo",
"bar",
"bat",
100, /*bam*/
null);
但我得到以下错误:
but I get the following error:
参数1:无法从方法组转换
System.Func
Argument 1: cannot convert from 'method group' to 'System.Func'
似乎没有合适的重载FromAsync方法<一href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx\">http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx,它最多只支持5个参数(包括回调状态)上的BeginXXX方法。
It seems there is no suitable overloaded FromAsync method http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx, it only supports up to 5 arguments (including callback and state) on the BeginXXX method.
除了重构的BeginXXX方法取一个对象,而不是六个参数,有没有使用FromAsync执行它的方式?
Other than refactoring the BeginXXX method to take an object rather than six arguments, is there a way to execute it using FromAsync?
推荐答案
其实我似乎可以使用Factory.FromAsync重载方法(即需要一个IAsyncResult对象作为第一个参数和一个回调方法作为第二:
Actually it seems I can use the overloaded method for Factory.FromAsync( that takes an IAsyncResult object as the first argument and a callback method as the second:
result = Task<string>.Factory.FromAsync(
instance.BeginGetMyNumber("foo", "bar", "bat", 1, null, null),
instance.EndGetMyNumber);
这篇关于太多的论点为的BeginXXX FromAsync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!