
14.03.2009, 17:18
|
|
Участник форума
Регистрация: 07.07.2008
Сообщений: 161
С нами:
9391926
Репутация:
234
|
|
.::BARS::.
Для синхронизации потоков используется мьютекс:
Код:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace file_thread
{
class Program
{
public static StreamWriter wr;
static void Main(string[] args)
{
wr = new StreamWriter("output.txt");
Thread t1 = new Thread(new ThreadStart(Thread1));
Thread t2 = new Thread(new ThreadStart(Thread2));
t1.Start();
t2.Start();
}
public static void Thread1()
{
lock(wr)
{
for (int i = 0; i < 1000; i++)
{
wr.WriteLine("Thread #1");
}
}
}
public static void Thread2()
{
lock (wr)
{
for (int i = 0; i < 1000; i++)
{
wr.WriteLine("Thread #2");
}
}
}
}
}
|
|
|