如何防止 System.Timers.Timer 在停止后引发 Elapsed 事件?

发布于 2024-11-01 09:02:17 字数 914 浏览 0 评论 0原文

我有以下代码:

using (FileStream sourceStream = sourceFile.Open())
{
    using (FileStream targetStream = File.Create(targetFilePath))
    {
        using (Timer timer = new Timer(250))
        {
            timer.Elapsed += (sender, e) =>
            {
                if (this.FileCopyProgressChanged != null)
                {
                    //Here the ObjectDisposedException appears
                    this.FileCopyProgressChanged(this,
                        new CopyProgressEventArgs(
                            sourceStream.Length,
                            targetStream.Length));
                }
            };

            timer.Start();

            sourceStream.CopyTo(targetStream);

            timer.Stop();
        }
    }
}

我的计时器每 250 毫秒计时一次,并引发一个事件,其中包含有关流复制进度的信息。 问题是,有时在计时器事件中,会抛出 ObjectDisposeException,因为流不再打开。

如何防止我的计时器在我的流被处理后引发已用事件?

I have the followig code:

using (FileStream sourceStream = sourceFile.Open())
{
    using (FileStream targetStream = File.Create(targetFilePath))
    {
        using (Timer timer = new Timer(250))
        {
            timer.Elapsed += (sender, e) =>
            {
                if (this.FileCopyProgressChanged != null)
                {
                    //Here the ObjectDisposedException appears
                    this.FileCopyProgressChanged(this,
                        new CopyProgressEventArgs(
                            sourceStream.Length,
                            targetStream.Length));
                }
            };

            timer.Start();

            sourceStream.CopyTo(targetStream);

            timer.Stop();
        }
    }
}

My timer elapses every 250 milliseconds and raises an event with information about the stream copy progress.
The problem is, that sometimes in the timer event, a ObjectDisposedException gets thrown, because the streams are not opened anymore.

How can I prevent my timer from raising the elapsed event after my streams are disposed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷月断魂刀 2024-11-08 09:02:17

为什么不直接这样做,不需要计时器:

using (FileStream sourceStream = sourceFile.Open())
{
    using (FileStream targetStream = File.Create(targetFilePath))
    {
        int bytesTotal = sourceStream.Length;
        int bytesCurrent = 0;
        var buffer = new byte[32*1024];
        int bytes;
        while ((bytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            targetStream.Write(buffer, 0, bytes);
            bytesCurrent += bytes;
            FileCopyProgressChanged(this,
                new CopyProgressEventArgs(bytesTotal, bytesCurrent));
        }
    }
}

Why not just do this, no timers required:

using (FileStream sourceStream = sourceFile.Open())
{
    using (FileStream targetStream = File.Create(targetFilePath))
    {
        int bytesTotal = sourceStream.Length;
        int bytesCurrent = 0;
        var buffer = new byte[32*1024];
        int bytes;
        while ((bytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            targetStream.Write(buffer, 0, bytes);
            bytesCurrent += bytes;
            FileCopyProgressChanged(this,
                new CopyProgressEventArgs(bytesTotal, bytesCurrent));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文