如何在Task中传递多个参数

11

我有一个函数 GetPivotedDataTable(data, "date", "id", "flag") 返回的是数据透视表格式。我想使用Task调用此方法,但如何传递多个参数到Task中。

5个回答

30

您可以使用lambda表达式或Func来传递参数:)

public Form1()
{
    InitializeComponent();

    Task task = new Task(() => this.GetPivotedDataTable("x",DateTime.UtcNow,1,"test"));
    task.Start();
}

public void GetPivotedDataTable(string data, DateTime date, int id, string flag)
{
    // Do stuff
}

2
虽然要注意变量的变化(例如使用不同的ID启动多个任务),但您需要创建该变量的本地副本:https://dev59.com/9Wkw5IYBdhLWcg3wDWEk。 - SharpC

3

如果您的参数类型不同,可以使用对象数组,然后将其强制转换回原始类型。

请查看此控制台应用程序示例:

    static void Main(string[] args)
    {
        var param1String = "Life universe and everything";
        var param2Int = 42;

        var task = new Task((stateObj) =>
            {
                var paramsArr = (object[])stateObj; // typecast back to array of object

                var myParam1String = (string)paramsArr[0]; // typecast back to string 
                var myParam2Int = (int)paramsArr[1]; // typecast back to int 

                Console.WriteLine("");
                Console.WriteLine(string.Format("{0}={1}", myParam1String, myParam2Int));
            },
            new object[] { param1String, param2Int } // package all params in an array of object
        );

        Console.WriteLine("Before Starting Task");
        task.Start();
        Console.WriteLine("After Starting Task");

        Console.ReadKey(); 
    }

0

你也可以使用 "params"。查看 C# 的 params 信息

public class MyClass
{
    public static void UseParams(params int[] list)
    {
        for (int i = 0; i < list.Length; i++)
        {
            Console.Write(list[i] + " ");
        }
        Console.WriteLine();
    }

    public static void UseParams2(params object[] list)
    {
        for (int i = 0; i < list.Length; i++)
        {
            Console.Write(list[i] + " ");
        }
        Console.WriteLine();
    }

    static void Main()
    {
        // You can send a comma-separated list of arguments of the 
        // specified type.
        UseParams(1, 2, 3, 4);
        UseParams2(1, 'a', "test");

        // A params parameter accepts zero or more arguments.
        // The following calling statement displays only a blank line.
        UseParams2();

        // An array argument can be passed, as long as the array
        // type matches the parameter type of the method being called.
        int[] myIntArray = { 5, 6, 7, 8, 9 };
        UseParams(myIntArray);

        object[] myObjArray = { 2, 'b', "test", "again" };
        UseParams2(myObjArray);

        // The following call causes a compiler error because the object
        // array cannot be converted into an integer array.
        //UseParams(myObjArray);

        // The following call does not cause an error, but the entire 
        // integer array becomes the first element of the params array.
        UseParams2(myIntArray);
    }
}


/*
Output:
    1 2 3 4
    1 a test

    5 6 7 8 9
    2 b test again
    System.Int32[]
*/

0
你可以创建一个“helper”类,其中包含你在任务中所需的所有参数。

-1

你可以使用元组

Task<Tuple<Tuple<Parame1,Parame2,Parame....>> Func()
{

}


var r = await Func();
r.Item1;
r.Item2;
r.Item.....

当您回答问题时,请解释一下您的解决方案如何解决问题。 - Balagurunathan Marimuthu
这是一个有效的答案。 - user1529413

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接