public void FileLockedByThisProcess()
{
using (var tester = new OptimisticLockedTextFileTester(true))
{
// start the persist thread
Thread thread = new Thread(new ThreadStart(() => { tester.TextFile.Persist(); }));
thread.Start();
// give it time to open the file
Thread.Sleep(2000);
// try something that requires minimal access to the file and make sure the correct exception is thrown
AssertExtensions.ExpectException(() =>
{
using (File.Open(tester.TextFile.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
}
}, typeof(IOException), string.Format(FileLockedMessageFormat, tester.TextFile.FilePath));
// wait for the persist to complete
thread.Join();
}
}