C#Winforms Backgroundworker错误 - 未将对象设置为对象

时间:2018-05-31 09:41:00

标签: c# backgroundworker

我有一个winforms文件搜索应用程序,在visual studio中没有错误运行,但是当我发布它时,我得到一个“对象未设置为对象错误的实例”

背景工作者代码失败的地方是:

    private void backGroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        e.Result = RunSearch();
        PopulateUi((FilteredList)e.Result);
    }

    private void backGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        try
        {
                var ans = (FilteredList)e.Result;
                toolStripStatusLabel1.Text += "Folders found: " + ans.FolderCount + " | Files found: " + ans.FileCount;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

消息框显示异常文本。我需要做些什么来纠正这个问题?

修改 这个问题与上面提到的关于并发问题的问题不同,而不仅仅是一个对象为空。如上所述,应用程序在发布之前运行时没有错误。

感谢

编辑2

我尝试在类中创建一个私有对象(_x)并将结果分配给它,但它仍然在RunWorkerCompleted函数中显示为null。编辑后的代码如下所示:

    private void backGroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        e.Result = RunSearch();
        _x = e.Result;
        PopulateUi((FilteredList)e.Result);
    }

    private void backGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        try
        {
            if (_x == null)
            {
                MessageBox.Show("e is null");
            }

            var ans = (FilteredList)_x;
            toolStripStatusLabel1.Text += "Folders found: " + ans.FolderCount + " | Files found: " + ans.FileCount;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

0 个答案:

没有答案