0

我将 WebClient 子类化以更好地控制它的功能(更改超时,并提供一个事件处理程序,每 1MB 进度触发一次)。它连续运行(一次/分钟)从 XML 源轮询数据,并且大部分时间都可以正常工作。但是,有时我会收到一个我不理解的 ObjectDisposedException。我将首先发布我认为是相关部分的评论,然后发布整个源代码以供参考。

DownloadProgressChanged 事件处理程序和处置

ObjectDisposedException 发生在此处理程序的最后一行。变量处于注释中指示的状态。

void MyWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    // cancelDueToError is false, isDisposed is true
    // isDisposed must have been false when this event handler fired
    if (cancelDueToError || isDisposed) return; 

    long additionalBytesReceived = e.BytesReceived - PreviousBytesReceived;
    PreviousBytesReceived = e.BytesReceived;
    BytesNotNotified += additionalBytesReceived;

    if (BytesNotNotified > ONE_MB)
    {
        OnNotifyMegabyteIncrement(e.BytesReceived);
        BytesNotNotified = 0;    // This is 0 at the point of the Exception, so this handler fired. 
    }
    firstByteReceived = true;

            //
            // !!!!! EXCEPTION HERE
            //
    // ObjectDisposedException here.  Clearly the class has been disposed already (isDisposed is true).  
    // But why is the DownloadProgressChanged event firing after MyWebClient is disposed?
    abortTimer.Change(TimeBetweenProgressChanges, System.Threading.Timeout.Infinite); 
}

// Somehow IDisposable.Dispose() is being called in the middle of the
// MyWebClient_DownloadProgressChanged event handler.  isDisposed is false
// at the beginning of the event handler (else it would have returned immediately)
// but true when I get the ObjectDisposedException
void IDisposable.Dispose()
{
    isDisposed = true;

    if (asyncWait != null) asyncWait.Dispose();
    if (abortTimer != null) abortTimer.Dispose();

    base.Dispose();
}

用法

using (MyWebClient webClient = new MyWebClient())
{
    webClient.NotifyMegabyteIncrement += new MyWebClient.PerMbHandler(webClient_NotifyMegabyteIncrement);
    bool downloadOK = webClient.DownloadFileWithEvents(url, outputPath);
    // Do stuff with downloaded file if downloadOK
}   

显然,有时,MyWebClient 被放置在 MyWebClient_DownloadProgressChanged 事件处理程序的中间。但是,在下载完成之前不应释放该对象。这怎么可能发生?

完整的源代码

public class MyWebClient : WebClient, IDisposable
{
    public int Timeout { get; set; }
    public int TimeUntilFirstByte { get; set; }
    public int TimeBetweenProgressChanges { get; set; }

    public long PreviousBytesReceived { get; private set; }
    public long BytesNotNotified { get; private set; }

    public string Error { get; private set; }
    public bool HasError { get { return Error != null; } }

    private bool firstByteReceived = false;
    private bool success = true;
    private bool cancelDueToError = false;

    private EventWaitHandle asyncWait = new ManualResetEvent(false);
    private Timer abortTimer = null;
    private bool isDisposed = false;

    const long ONE_MB = 1024 * 1024;

    public delegate void PerMbHandler(long totalMb);

    public event PerMbHandler NotifyMegabyteIncrement;

    public MyWebClient(int timeout = 60000, int timeUntilFirstByte = 30000, int timeBetweenProgressChanges = 15000)
    {
        this.Timeout = timeout;
        this.TimeUntilFirstByte = timeUntilFirstByte;
        this.TimeBetweenProgressChanges = timeBetweenProgressChanges;

        this.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(MyWebClient_DownloadFileCompleted);
        this.DownloadProgressChanged += new DownloadProgressChangedEventHandler(MyWebClient_DownloadProgressChanged);

        abortTimer = new Timer(AbortDownload, null, TimeUntilFirstByte, System.Threading.Timeout.Infinite);
    }

    protected void OnNotifyMegabyteIncrement(long totalMb)
    {
        if (NotifyMegabyteIncrement != null) NotifyMegabyteIncrement(totalMb);
    }

    void AbortDownload(object state)
    {
        cancelDueToError = true;
        this.CancelAsync();
        success = false;
        Error = firstByteReceived ? "Download aborted due to >" + TimeBetweenProgressChanges + "ms between progress change updates." : "No data was received in " + TimeUntilFirstByte + "ms";
        asyncWait.Set();
    }

    void MyWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        if (cancelDueToError || isDisposed) return;

        long additionalBytesReceived = e.BytesReceived - PreviousBytesReceived;
        PreviousBytesReceived = e.BytesReceived;
        BytesNotNotified += additionalBytesReceived;

        if (BytesNotNotified > ONE_MB)
        {
            OnNotifyMegabyteIncrement(e.BytesReceived);
            BytesNotNotified = 0;
        }
        firstByteReceived = true;
        abortTimer.Change(TimeBetweenProgressChanges, System.Threading.Timeout.Infinite);
    }

    public bool DownloadFileWithEvents(string url, string outputPath)
    {
        asyncWait.Reset();
        Uri uri = new Uri(url);
        this.DownloadFileAsync(uri, outputPath);
        asyncWait.WaitOne();

        return success;
    }

    void MyWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (cancelDueToError || isDisposed) return;
        asyncWait.Set();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {            
        var result = base.GetWebRequest(address);
        result.Timeout = this.Timeout;
        return result;
    }

    void IDisposable.Dispose()
    {
        isDisposed = true;

        if (asyncWait != null) asyncWait.Dispose();
        if (abortTimer != null) abortTimer.Dispose();

        base.Dispose();
    }
}
4

0 回答 0