|
|
От: |
nikov
|
http://www.linkedin.com/in/nikov |
| Дата: | 16.08.06 16:52 | ||
| Оценка: | |||
using System;
using System.IO;
using System.Threading;
public class Program
{
private static readonly Random _random = new Random();
public static void Main()
{
while (true)
{
Thread thread = new Thread(DoWork);
thread.Start();
Thread.Sleep(_random.Next(100));
thread.Abort();
}
}
public static void DoWork()
{
while (true)
{
using (FileStream fs = new FileStream("temp.txt", FileMode.Create))
{
}
}
}
}Unhandled Exception: System.IO.IOException: The process cannot access the file 'temp.txt' because it is being used by another process.
using System;
using System.IO;
using System.Threading;
public class Program
{
private static readonly Random _random = new Random();
public static void Main()
{
while (true)
{
Thread thread = new Thread(DoWork);
thread.Start();
Thread.Sleep(_random.Next(100));
thread.Abort();
}
}
public static void DoWork()
{
while (true)
{
FileStream fs = null;
try
{
try
{
Thread.Sleep(0);
}
finally
{
fs = new FileStream("temp.txt", FileMode.Create);
}
}
finally
{
if(fs != null) ((IDisposable) fs).Dispose();
}
}
}
}