为什么文件流返回的长度为0?

时间:2015-09-13 02:05:09

标签: c# .net .net-4.5 visual-studio-2015

所以我一直在遇到溢出异常,我发现这是错误的评估。但是当我设置一个断点时,代码中的fs.Length - startAt的结果等于startAt的精确倒数,但是在观察窗口中,同样的确切事物评估为我期望的数字。

enter image description here

这里发生了什么?我重新启动了visual studio;没有变化。

private async Task DoTail()
{
    await Task.Run(async () =>
    {
        while (!_cts.IsCancellationRequested)
        {
            await UpdateFile();
            await Task.Delay(_interval);
        }
    }, _cts.Token);

    Debug.WriteLine($"Stopped tailing {LogInfo.Alias}");
}

private async Task UpdateFile()
{
    try
    {
        if (!File.Exists(LogInfo.Location))
        {
            Trace.WriteLine($"{LogInfo.Location} was not found.");
            Contents = $"File not found: {LogInfo.Location}";
            _cts.Cancel();
            return;
        }

        using (var fs = new FileStream(LogInfo.Location, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            if (!fs.CanRead || fs.Length == _lastIndex) return; // no change

            // avoid reading the entire file on startup
            long startAt = _lastIndex;
            if (startAt == 0 && fs.Length > _displayBuffer)
            {
                startAt = fs.Length - _displayBuffer;
                Trace.WriteLine($"File was larger than buffer ({_displayBuffer}). Starting at i={startAt} instead of beginning.");
            }

            long contentLength = fs.Length - startAt;
            if (contentLength < 0)
            {
                // trying to track some weird overflow case
                Trace.WriteLine($"!!!!!! Length: {fs.Length} startAt: {startAt}");
                return;
            }

            var newContent = new byte[contentLength];
            Trace.WriteLine($"This chunk will be {newContent.Length} bytes.");

....
}

0 个答案:

没有答案