Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步任务。运行时不工作_C#_Asynchronous_Async Await - Fatal编程技术网

C# 异步任务。运行时不工作

C# 异步任务。运行时不工作,c#,asynchronous,async-await,C#,Asynchronous,Async Await,我只是写了下面的代码,我希望在C#中有3个具有异步功能的文本文件,但我没有看到任何内容: private async void Form1_Load(object sender, EventArgs e) { Task<int> file1 = test(); Task<int> file2 = test(); Task<int> file3 = test();

我只是写了下面的代码,我希望在C#中有3个具有异步功能的文本文件,但我没有看到任何内容:

  private async void Form1_Load(object sender, EventArgs e)
        {
            Task<int> file1 = test();
            Task<int> file2 = test();
            Task<int> file3 = test();
            int output1 = await file1;
            int output2 = await file2;
            int output3 = await file3;

        }

  async Task<int> test()
        {
            return await Task.Run(() =>
            {
                string content = "";
                for (int i = 0; i < 100000; i++)
                {
                    content += i.ToString();
                }
                System.IO.File.WriteAllText(string.Format(@"c:\test\{0}.txt", new Random().Next(1, 5000)), content);
                return 1;
            });
        }
private async void Form1\u加载(对象发送方,事件参数e)
{
任务文件1=测试();
任务文件2=测试();
任务文件3=测试();
int output1=等待文件1;
int output2=等待文件2;
int output3=等待文件3;
}
异步任务测试()
{
返回等待任务。运行(()=>
{
字符串内容=”;
对于(int i=0;i<100000;i++)
{
content+=i.ToString();
}
System.IO.File.WriteAllText(string.Format(@“c:\test\{0}.txt”,new Random().Next(15000)),content);
返回1;
});
}
每次使用不同的实例将导致随机数生成每次生成相同的数

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. 
这是因为Random使用计算机的时间作为种子值,但其精度不足以满足计算机的处理速度

使用相同的随机数生成器,例如:

internal async Task<int> test()
{
    return await Task.Run(() =>
    {
        string content = "";
        for (int i = 0; i < 10000; i++)
        {
            content += i.ToString();
        }
        System.IO.File.WriteAllText(string.Format(@"c:\test\{0}.txt",MyRandom.Next(1,5000)), content);
        return 1;
    });
}

有几个潜在问题:

  • c:\test\
    是否存在?否则,您将得到一个错误
  • 正如所写的,您的对象可能会生成相同的数字,因为当前系统时间被用作种子,并且您几乎在同一时间执行这些操作。您可以通过让它们共享一个
    静态随机
    实例来解决这个问题。编辑:。我在
    Random
    实例上选择了一个简单的
    lock
    ,它不是最快的,但适用于本例
  • 以这种方式构建长
    字符串是非常低效的(例如,对于我来说,调试模式下大约43秒,只需一次)。您的任务可能工作得很好,但您没有注意到它实际上在做任何事情,因为它需要很长时间才能完成。通过使用该类(例如,大约20毫秒),可以使其速度更快
  • (这不会影响它是否工作,但更多的是一种风格)您不需要在编写的
    test()
    方法中使用
    async
    wait
    关键字。它们是多余的,因为
    Task.Run
    已经返回
    任务
  • 这对我很有用:

    private async void Form1_Load(object sender, EventArgs e)
    {
        Task<int> file1 = test();
        Task<int> file2 = test();
        Task<int> file3 = test();
        int output1 = await file1;
        int output2 = await file2;
        int output3 = await file3;
    
    }
    static Random r = new Random();
    Task<int> test()
    {
        return Task.Run(() =>
        {
            var content = new StringBuilder();
            for (int i = 0; i < 100000; i++)
            {
                content.Append(i);
            }
            int n;
            lock (r) n = r.Next(1, 5000);
            System.IO.File.WriteAllText(string.Format(@"c:\test\{0}.txt", n), content.ToString());
            return 1;
        });
    }
    
    private async void Form1\u加载(对象发送方,事件参数e)
    {
    任务文件1=测试();
    任务文件2=测试();
    任务文件3=测试();
    int output1=等待文件1;
    int output2=等待文件2;
    int output3=等待文件3;
    }
    静态随机r=新随机();
    任务测试()
    {
    返回任务。运行(()=>
    {
    var content=新的StringBuilder();
    对于(int i=0;i<100000;i++)
    {
    内容。附加(i);
    }
    int n;
    锁(r)n=r.Next(15000);
    System.IO.File.WriteAllText(string.Format(@“c:\test\{0}.txt”,n),content.ToString();
    返回1;
    });
    }
    
    也许您需要同步随机实例+1@Jaycee锁定特定对象时,通常可以直接使用该对象(例如,
    lock(random)
    ),无需(名称混淆)
    。要同步对random的访问
    private async void Form1_Load(object sender, EventArgs e)
    {
        Task<int> file1 = test();
        Task<int> file2 = test();
        Task<int> file3 = test();
        int output1 = await file1;
        int output2 = await file2;
        int output3 = await file3;
    
    }
    static Random r = new Random();
    Task<int> test()
    {
        return Task.Run(() =>
        {
            var content = new StringBuilder();
            for (int i = 0; i < 100000; i++)
            {
                content.Append(i);
            }
            int n;
            lock (r) n = r.Next(1, 5000);
            System.IO.File.WriteAllText(string.Format(@"c:\test\{0}.txt", n), content.ToString());
            return 1;
        });
    }