Backgroundworker线程抛出nullreferenceexception

时间:2009-10-01 16:03:28

标签: c# backgroundworker

我正在使用BackgroundThread加载图像。在将所有图像加载到Listview后,我正在接收“由用户代码未处理的”nullreferenceexception“。可能是什么问题?请告诉我。

   private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            int progress = 0;

            string pname;
            Image myImage;
            max_length = files.Length;
            for (int k = 0; k < files.Length; k++)
            {
                ProgressInfo info = new ProgressInfo();
               pname = System.IO.Path.GetFullPath(files[k]);
                myImage = Image.FromFile(pname);
                info.Image = myImage;
                info.ImageIndex = k;
                backgroundWorker1.ReportProgress(progress, info);
                myImage = null;
            }
        }
        catch (Exception ex)
        {
            throw ex.InnerException;
        }
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

            try
            {

                //Get image.
                ProgressInfo img = e.UserState as ProgressInfo;
                //Set image to ListView here.
                ImgListView.Images.Add(getThumbnaiImage(ImgListView.ImageSize.Width, img.Image));
                fname = System.IO.Path.GetFileName(files[img.ImageIndex]);
                ListViewItem lvwItem = new ListViewItem(fname, img.ImageIndex);
                lvwItem.Tag = files[img.ImageIndex];

                lstThumbNailView.Items.AddRange(new ListViewItem[] { lvwItem });

                percent = (int)((float)100 * (float)i / (float)files.Length);
                this.progressBar1.Value = (int)percent;
                 this.label1.Text = "Loading images...";

            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
    }

2 个答案:

答案 0 :(得分:2)

根据您的评论判断,您会看到错误,因为并非所有异常都有InnerException。如果InnerException为null,您将看到此问题。

但是这里有几个问题在起作用。首先,这是正确的try / catch方法:

try
{
  // Code here
}
catch (Exception ex)
{
  // Handle your exception
  throw; // Rethrow the same exception, preserving the stack trace (optional)
}

其次,您可能会滥用ReportProgress的目的。您应该尝试在backgroundWorker_DoWork中执行所有逻辑,并将百分比(介于0和100之间)发送到ReportProgress以更新任何进度条。

您可能已经按照修复多线程问题的方式使用了ReportProgress。要跨线程将项添加到ListBox,请使用BeginInvoke函数将代码包装在匿名方法中

示例:

// Note: I haven't error checked this, and this is only a rough idea of what 
// you're trying to do. I'm not even sure of the overall goal here.
this.lstThumbnailView.Invoke((Action)delegate
{
  ListViewItem lvwItem = new ListViewItem(name, img.ImageIndex);
  ivwItem.Tag = files[img.ImageIndex];
  lstThumbNailView.Items.Add(lvwItem);
});

答案 1 :(得分:0)

感谢您的快速回复。以下是我所做的更改。它似乎工作正常。

   private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

            try
            {
                ProgressInfo img = e.UserState as ProgressInfo;
                LoadImages(img);
                this.progressBar1.Value = img.Percent;
                this.label1.Text = "Loading images...";

            }
            catch (Exception ex)
            {
               throw ex;
            }
    }

    private void LoadImages(ProgressInfo img)
    {
        ImgListView.Images.Add(getThumbnaiImage(ImgListView.ImageSize.Width, img.Image));
        this.lstThumbNailView.Invoke((Action)delegate
        {
            fname = System.IO.Path.GetFileName(files[img.ImageIndex]);
            ListViewItem lvwItem = new ListViewItem(fname, img.ImageIndex);
            lvwItem.Tag = files[img.ImageIndex]; 
            lstThumbNailView.Items.Add(lvwItem); 
        });
    }