私は次のコードを持っています:
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 ミリ秒ごとに経過し、ストリーム コピーの進行状況に関する情報を含むイベントを発生させます。問題は、ストリームがもう開かれていないため、タイマー イベントで ObjectDisposedException がスローされることがあります。
ストリームが破棄された後にタイマーが経過イベントを発生させないようにするにはどうすればよいですか?