Это своего рода учебный пример.Попытка сделать output.txt вроде
counter + 0
counter + 1
и т. Д.написав каждую строку в отдельном потоке.
using (StreamWriter output = File.CreateText("output.txt"))
{
object block = new object();
for (int i = 0; i < 10; i++)
{
Action<object> writeFunction = delegate (object obj)
{
lock (block)
{
int counter = (int) obj;
output.WriteLine("counter + " + counter);
//output.Flush();
}
};
Thread newThread = new Thread(new ParameterizedThreadStart(writeFunction));
newThread.Start(i);
}
}
Закомментированный вывод. Функция Flush () вызывает System.ObjectDisposedException «Доступ к закрытому файлу невозможен».