C# 异步任务。运行时不工作
我只是写了下面的代码,我希望在C#中有3个具有异步功能的文本文件,但我没有看到任何内容: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();
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;
});
}