我刚试过这个,但失败了
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
writeFile();
});
}
TextWriter myWriter = new StreamWriter("deneme.txt");
void writeFile()
{
for (int k = 0; k < 10000; k++)
{
int irTempPara = k;
Task.Factory.StartNew(() =>
{
writeFileForReal(irTempPara);
});
System.Threading.Thread.Sleep(10);
}
}
void writeFileForReal(int srParameter)
{
for (int k = 0; k < 999999999; k++)
{
myWriter.WriteLineAsync(srParameter + "_" + k);
}
}
它是 c# 4.5 WPF 应用程序
那么你会建议什么方法最有效地和多线程将行写入文本文件