0

你好我有以下问题:

我想执行类似于事务的操作。我想async在收到外部触发器后执行一些操作。因此,我使用的TaskCompletionSource是在表示触发器的方法中设置的 : TriggerTransaction。 当我按下特定的控制台键时,

会在线程池中调用此触发方法。Main

在我按下A关键字后,TriggerTransaction执行并设置TaskCompletionSource-s。仍然主线程不计算两个等待任务的总和。

     class Program
                {
                    public static Task<Task<int>> TransactionOperation1()
                    {
                        TaskCompletionSource<Task<int>> tcs = new TaskCompletionSource<Task<int>>();
                        tasks.Add(tcs);
                        Task<Task<int>> result = tcs.Task;
                        return result;
                    }
                    public static Task<Task<int>> TransactionOperation2()
                    {
                        TaskCompletionSource<Task<int>> tcs = new TaskCompletionSource<Task<int>>();
                        tasks.Add(tcs);
                        Task<Task<int>> result = tcs.Task;
                        return result;
                    }

                    public static async Task<int> ExecuteTransactionOnDB()
                    {
                        await Task.Delay(1000);
                        return 5;
                    }

                    public static async Task TriggerTransaction()
                    {
                        int value = await ExecuteTransactionOnDB();
                        foreach (var item in tasks)
                        {
                            item.SetResult(value);
                        }
                    }
                    public static List<dynamic> tasks = new List<dynamic>();

                    static async Task Main(string[] args)
                    {
                        Task<Task<int>> a = TransactionOperation1();
                        Task<Task<int>> b = TransactionOperation2();
                        Task.Run(async() =>
                        {
                            while (Console.ReadKey().Key != ConsoleKey.A) ;
                            await TriggerTransaction();
                        });
                        if (!File.Exists("D:\\data.txt"))
                        {
                            File.Create("D:\\data.txt");
                        }
                        using(FileStream stream=new FileStream("data.txt",FileMode.Append,FileAccess.Write))
                        {
                        int sum=await await a + await await b;//thread wont pass this line when tasks are set.
                        ReadOnlyMemory<byte> bytes = Encoding.UTF8.GetBytes(sum);

                            stream.Write(bytes.ToArray());
                        }
                        Console.WriteLine(await await a + await await b);

                    }
                }
        }

PS如果你想知道为什么我确实使用 aList<dynamic>来存储TaskCompletionSource-s ,那是因为 TransactionOperations 的返回类型会有所不同。其中一些会返回int,其他String.. ..etc Bool

为了更好地理解,我制作了一个架构-正如您将看到的那样:
-我要存储 TCS-es 的列表-
仅在设置外部触发器后才完成的某些调用(事务已执行)
您可以在 中看到Calls,都有不同的返回类型。
在此处输入图像描述

4

2 回答 2

1

为什么你需要一个Task<Task<int>>?简单Task<int>就足够了,因此,TaskCompletionSource<int>. 而且您还摆脱了尴尬await await ...,这在您的情况下也不是必需的。

请注意,我还添加Close()到由File.Create().

这是该程序的工作版本:

class Program
{
    public static Task<int> TransactionOperation1()
    {
        TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
        tasks.Add(tcs);
        return tcs.Task;
    }
    public static Task<int> TransactionOperation2()
    {
        TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
        tasks.Add(tcs);
        return tcs.Task;
    }
    public static async Task<int> ExecuteTransactionOnDB()
    {
        await Task.Delay(1000);
        return 5;
    }
    public static async Task TriggerTransaction()
    {
        int value = await ExecuteTransactionOnDB();
        foreach (var item in tasks)
        {
            item.SetResult(value);
        }
    }

    public static List<dynamic> tasks = new List<dynamic>();

    static async Task Main(string[] args)
    {
        Task<int> a = TransactionOperation1();
        Task<int> b = TransactionOperation2();
        Task input = Task.Run(async () => {
            while (Console.ReadKey().Key != ConsoleKey.A);
            await TriggerTransaction();
        });
        if (!File.Exists("C:\\temp\\data.txt"))
        {
            File.Create("C:\\temp\\data.txt").Close();
        }
        using (FileStream stream = new FileStream("C:\\temp\\data.txt", FileMode.Append, FileAccess.Write))
        {
            int sum = await a + await b; // now it works ok
            var bytes = Encoding.UTF8.GetBytes(sum.ToString());
            stream.Write(bytes);
        }

        Console.WriteLine(await a + await b);
    }
}
于 2018-08-02T07:58:23.673 回答
1

查看代码的修改版本,它通过执行Task创建的 using产生预期的结果TaskCompletionSource。我也将代码设为通用代码,因此您无需dynamic在编译时使用类型和定义数据类型

static async Task Main(string[] args)
{
    var a = Program<int>.TransactionOperation1();
    var b = Program<int>.TransactionOperation2();
    await Task.Run(async() =>
    {
    Console.ReadLine();
    await Program<int>.TriggerTransaction(5);
    });

    if (!File.Exists("D:\\data.txt"))
    {
    File.Create("D:\\data.txt");
    }

    using (FileStream stream = new FileStream("D:\\data.txt", FileMode.Append, FileAccess.Write))
    {
        int sum = await a + await b;//thread wont pass this line when tasks are set.        
        var bytes = Encoding.UTF8.GetBytes(sum.ToString());     
        stream.Write(bytes, 0, bytes.Length);
    }

    Console.WriteLine(await a + await b);
}

class Program<T>
{
    public static Task<T> TransactionOperation1()
    {
        var tcs = new TaskCompletionSource<T>();
        tasks.Add(tcs);
        return tcs.Task;
    }

    public static Task<T> TransactionOperation2()
    {
        var tcs = new TaskCompletionSource<T>();
        tasks.Add(tcs);
        return tcs.Task;
    }

    public static async Task<T> ExecuteTransactionOnDB(T t)
    {
        return await Task.FromResult(t);
    }

    public static async Task TriggerTransaction(T t)
    {
        T value = await ExecuteTransactionOnDB(t);

        foreach (var item in tasks)
        {
            item.SetResult(value);
        }
    }

    public static List<TaskCompletionSource<T>> tasks = new List<TaskCompletionSource<T>>();

}

以下是重要的修改:

  1. List<dynamic>被替换为List<TaskCompletionSource<T>>
  2. TransactionOperation1/2有返回类型Task<T>,这是使用创建的任务TaskCompletionSource<T>
  3. Task.Run向内部添加了一个额外的等待TriggerTransaction,尽管您可以替换以下代码:

     await Task.Run(async() =>
     {
       Console.ReadLine();
       await Program<int>.TriggerTransaction(5);
     });
    

    await Program<int>.TriggerTransaction(5);

现在它产生了你所期望的结果,它将两个整数相加。一些小的更改,例如删除Task.Delay,这不是必需的

编辑 1 - 使用 Task.WhenAll

static async Task Main(string[] args)
{
    var a = Program.TransactionOperation1(5);
    var b = Program.TransactionOperation1(5);

    Console.ReadLine();

    var taskResults  = await Task.WhenAll(a,b);


    dynamic finalResult = 0;

    foreach(var t in taskResults)
        finalResult += t;


    if (!File.Exists("D:\\data.txt"))
    {
        File.Create("D:\\data.txt");
    }

    using (FileStream stream = new FileStream("D:\\data.txt", FileMode.Append, FileAccess.Write))
    {
        var bytes = Encoding.UTF8.GetBytes(finalResult.ToString());
        stream.Write(bytes, 0, bytes.Length);
    }

    Console.WriteLine(finalResult);
}

class Program
{
    public static Task<dynamic> TransactionOperation1(dynamic val)
    {
        return Task<dynamic>.Run(() => val);
    }

    public static Task<dynamic> TransactionOperation2(dynamic val)
    {
        return Task<dynamic>.Run(() => val);
    }

}
于 2018-08-02T08:13:20.077 回答