太多的论点的BeginXXX的FromAsync?(Too many arguments in Be

2019-08-17 21:41发布

我有以下签名异步方法:

IAsyncResult BeginGetMyNumber(string foo, string bar, string bat, int bam, AsyncCallback callback, object state)

我想用Factory.FromAsync这样来执行它:

var result  = Task<int>.Factory.FromAsync(
                instance.BeginGetMyNumber, 
                instance.EndGetMyNumber, 
                "foo",
                "bar",
                "bat",
                100, /*bam*/
                null);

但我得到以下错误:

参数1:不能从“方法组”转换为“System.Func”

似乎没有合适的重载FromAsync方法http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx ,它只到5个参数(包括回调和状态)支持高达上的BeginXXX方法。

除了重构的方法的BeginXXX采取的对象,而不是六个参数,有没有使用FromAsync执行它的方法吗?

Answer 1:

result = Task<string>.Factory.FromAsync(
  (callback, state) => instance.BeginGetMyNumber("foo", "bar", "bat", 1, callback, state),
   instance.EndGetMyNumber);

这种技术(部分功能应用程序)适用于开始任何数量的输入则params的方法。



Answer 2:

实际上似乎我可以使用Factory.FromAsync重载方法(其采用IAsyncResult对象作为第一个参数和回调方法作为第二:

result = Task<string>.Factory.FromAsync(
                instance.BeginGetMyNumber("foo", "bar", "bat", 1, null, null),
                instance.EndGetMyNumber);


Answer 3:

是的,基本上,你已经用完了论据。 :(

FromAsync方法只需要最多三个传递到最异步调用的参数,全面阐述了如下所示:

var result  = Task<int>
    .Factory
    .FromAsync<string,string,string>(
        BeginGetMyNumber,
        EndGetMyNumber,
        "foo",
        "bar",
        "bat",
        null);

如果你有这将工作:

IAsyncResult BeginGetMyNumber(
   string foo, 
   string bar, 
   string bat, 
   AsyncCallback callback, 
   object state)
{
}

但是ye've了太多了。

哦,得到的东西,可能会帮助- 你将要清理它,这是极其扔在一起!

public static class Ext
{
    public static Task<TResult> FromAsync<TArg1, TArg2, TArg3, TArg4, TResult>(
        this TaskFactory<TResult> factory,
        Func<TArg1,TArg2,TArg3,TArg4,AsyncCallback, object, IAsyncResult> beginMethod, 
        Func<IAsyncResult, TResult> endMethod, 
        TArg1 arg1,
        TArg2 arg2,
        TArg3 arg3,
        TArg4 arg4,
        object state,
        TaskCreationOptions creationOptions = TaskCreationOptions.None, 
        TaskScheduler scheduler = null)
    {
        scheduler = scheduler ?? TaskScheduler.Current;
        AsyncCallback callback = null;
        if (beginMethod == null)
        {
            throw new ArgumentNullException("beginMethod");
        }
        if (endMethod == null)
        {
            throw new ArgumentNullException("endMethod");
        }
        TaskCompletionSource<TResult> tcs = 
             new TaskCompletionSource<TResult>(state, creationOptions);
        try
        {
            if (callback == null)
            {
                callback = delegate (IAsyncResult iar) 
                {
                    tcs.TrySetResult(endMethod(iar));
                };
            }
            beginMethod(arg1, arg2, arg3, arg4, callback, state);
        }
        catch
        {
            tcs.TrySetResult(default(TResult));
            throw;
        }
        return tcs.Task;
    } 
}


文章来源: Too many arguments in BeginXXX for FromAsync?