为什么我的流在我自己做之前就被解锁了?
Why my stream is unlocked before I do it myself?
我有一个文本文件。多个进程可以同时尝试读取和编辑该文件。 FileStream.Unlock()
方法有问题:
using System;
using System.IO;
using System.Text;
static class Program
{
static void Main()
{
var fileName = @"c:\temp\data.txt";
// Content of the 'c:\temp\data.txt' file:
// Hello!
// The magic number is 000. :)))
// Good luck...
using (var stream = new FileStream(fileName, FileMode.Open,
FileAccess.ReadWrite, FileShare.ReadWrite))
{
using(var reader = new StreamReader(stream))
{
var value = 0;
Console.Write("New value [0-999]: ");
while(int.TryParse(Console.ReadLine(), out value))
{
var prevPosition = stream.Position;
stream.Position = 28;
var data = Encoding.UTF8.GetBytes(value.ToString());
try
{
stream.Lock(stream.Position, data.LongLength);
Console.WriteLine("Data locked. Press any key for continuation...");
Console.ReadKey();
stream.Write(data, 0, data.Length);
stream.Flush();
// I get the Exception here: The segment already unlocked.
stream.Unlock(stream.Position, data.LongLength);
}
catch(Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
stream.Position = prevPosition;
Console.Write("New value: ");
}
}
}
}
}
为什么我的流在我自己操作之前就被解锁了?
原因是 stream.Position
在您锁定文件(因为您写入文件)后提前,并且您使用 stream.Position
(现在不同)解锁文件。结果 - 您尝试解锁的范围与您锁定的范围不同。相反,保存 stream.Position
:
var position = stream.Position; // < save
stream.Lock(position, data.LongLength);
Console.WriteLine("Data locked. Press any key for continuation...");
stream.Write(data, 0, data.Length); // < this changes stream.Position, breaking your old logic
stream.Flush();
// I get the Exception here:
// The blocking of the segment already taken off.
stream.Unlock(position, data.LongLength); // < now you unlock the same range
不确定,但也许当您写入时,Stream.Position 会发生变化。
我有一个文本文件。多个进程可以同时尝试读取和编辑该文件。 FileStream.Unlock()
方法有问题:
using System;
using System.IO;
using System.Text;
static class Program
{
static void Main()
{
var fileName = @"c:\temp\data.txt";
// Content of the 'c:\temp\data.txt' file:
// Hello!
// The magic number is 000. :)))
// Good luck...
using (var stream = new FileStream(fileName, FileMode.Open,
FileAccess.ReadWrite, FileShare.ReadWrite))
{
using(var reader = new StreamReader(stream))
{
var value = 0;
Console.Write("New value [0-999]: ");
while(int.TryParse(Console.ReadLine(), out value))
{
var prevPosition = stream.Position;
stream.Position = 28;
var data = Encoding.UTF8.GetBytes(value.ToString());
try
{
stream.Lock(stream.Position, data.LongLength);
Console.WriteLine("Data locked. Press any key for continuation...");
Console.ReadKey();
stream.Write(data, 0, data.Length);
stream.Flush();
// I get the Exception here: The segment already unlocked.
stream.Unlock(stream.Position, data.LongLength);
}
catch(Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
stream.Position = prevPosition;
Console.Write("New value: ");
}
}
}
}
}
为什么我的流在我自己操作之前就被解锁了?
原因是 stream.Position
在您锁定文件(因为您写入文件)后提前,并且您使用 stream.Position
(现在不同)解锁文件。结果 - 您尝试解锁的范围与您锁定的范围不同。相反,保存 stream.Position
:
var position = stream.Position; // < save
stream.Lock(position, data.LongLength);
Console.WriteLine("Data locked. Press any key for continuation...");
stream.Write(data, 0, data.Length); // < this changes stream.Position, breaking your old logic
stream.Flush();
// I get the Exception here:
// The blocking of the segment already taken off.
stream.Unlock(position, data.LongLength); // < now you unlock the same range
不确定,但也许当您写入时,Stream.Position 会发生变化。