如果下面的代码适用于异步进程,我需要一些意见。此代码仅重新格式化文本。 如果此实施不正确或需要一些改进,请告诉我。
static void Main(string[] args)
{
Task.Factory.StartNew(() => ReadCharacter("File 1.txt"));
Task.Factory.StartNew(() => ReadCharacter("File 2.txt"));
Console.WriteLine("Main Task");
Console.ReadLine();
}
static void ReadCharacter(string inputFile)
{
string result;
using (StreamReader sr = new StreamReader(inputFile))
{
var sw = new StreamWriter(string.Format("C:\\Out--{0}",inputFile));
Console.WriteLine("Opening file : {0}", inputFile.ToString());
while (sr.EndOfStream == false)
{
result = sr.ReadLine();
sw.WriteLine(string.Format("{0} --> {1}",
result,Task.CurrentId.ToString()));
}
Console.WriteLine("Finish {0}", inputFile);
sr.Close(); sw.Close();
}
}