在具有多个ReportProgress调用的BackgroundWorker progressChanged事件中,e.UserState为null

时间:2014-12-10 17:43:47

标签: c# .net winforms

这是为ftp服务器下载文件的方法: 在下载方法时,我正在做:

var progress = bytesRead * 100.0 / writeStream.Length;
bw.ReportProgress((int)progress);

由于某种原因,在progressChanged事件中的form1中,即使我有报告,e.UserState也为null:

string SummaryText = String.Format("File Name {0} / {1}", "", filenameonly);
bw.ReportProgress(0, SummaryText);

没有第二次报告,那么e.UserState工作正常。

public void DownloadFtpContent(object sender ,string file, string filesdirectories,string fn)
{    
    try
    {     
        BackgroundWorker bw = sender as BackgroundWorker;
        string filenameonly = Path.GetFileName(file);
        string ftpdirectories = Path.Combine(ftpcontentdir, filesdirectories);
        string fileurl = "ftp://" + file;
        FtpWebRequest reqFTP;                
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(fileurl);                                
        reqFTP.Credentials = new NetworkCredential(UserName, Password);
        reqFTP.UseBinary = true;
        reqFTP.UsePassive = true;
        reqFTP.KeepAlive = true;                
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.Proxy = null;                 
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream responseStream = response.GetResponseStream();

        if (!Directory.Exists(ftpdirectories))
        {
            Directory.CreateDirectory(ftpdirectories);
        }
        FileStream writeStream = new FileStream(ftpdirectories + "\\" + filenameonly, FileMode.Create);
        string fnn = ftpdirectories + "\\" + filenameonly;
        int Length = 2048;
        Byte[] buffer = new Byte[Length];
        int bytesRead = responseStream.Read(buffer, 0, Length);
        string SummaryText = String.Format("File Name {0} / {1}", "", filenameonly);
        bw.ReportProgress(0, SummaryText);
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = responseStream.Read(buffer, 0, Length);
            var progress = bytesRead * 100.0 / writeStream.Length;
            bw.ReportProgress((int)progress);
        }               
        writeStream.Close();
        response.Close();
    }
    catch (WebException wEx)
    {
        //MessageBox.Show(wEx.Message, "Download Error");
    }
    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message, "Download Error");
    }
}

在progressChanged事件中的form1中,当我使用e.UserState时,我得到异常,说它为空:

private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.toolStripStatusLabel2.Text = e.UserState.ToString();
    this.toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum, e.ProgressPercentage);
}

第一次没问题,但是第二次进入ProgressChanged并尝试制作e.UserState线,我得到了例外。

例外是:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=FTP_ProgressBar
  StackTrace:
       at FTP_ProgressBar.Form1.backgroundWorker2_ProgressChanged(Object sender, ProgressChangedEventArgs e) in c:\ftp_progressbar\FTP_ProgressBar\Form1.cs:line 566
  InnerException: 

第566行是:this.toolStripStatusLabel2.Text = e.UserState.ToString();

1 个答案:

答案 0 :(得分:1)

我通过在while循环中的下载方法中只报告一次来解决这个问题:

string SummaryText = String.Format("File Name {0} / {1}", "", filenameonly);
while (bytesRead > 0)
{
    writeStream.Write(buffer, 0, bytesRead);
    bytesRead = responseStream.Read(buffer, 0, Length);
    var progress = bytesRead * 100.0 / writeStream.Length;
    bw.ReportProgress((int)progress, SummaryText);
}