没有设置`e.result`的背景工作者

时间:2013-06-06 13:52:25

标签: .net vb.net backgroundworker

概述:
我有几个后台工作程序用于将数据导入访问数据库,然后执行一些计算。我使用hashtablee.Argument在后​​台工作人员之间传递e.Result个参数。此哈希表包含有关要导入的文件的数据。

问题:
在我的一个后台工作者(在此过程中大约第四个),我无法设置e.result,而是保持为nothing,然后在工作完成的子例程中导致错误。据我所知,这是我的其他工作人员的所有代码的副本,所以没有理由不设置这个。

代码:

Private Sub bwConditionCalc_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwConditionCalc.DoWork
    Dim Dates As New List(Of Date)
    Dim Arguments As Hashtable
    Arguments = e.Argument

'Worker Content-----------

    Arguments("VariablesCalculated") = VariablesCalculated
    LabelString = "Tasks Complete..."
    e.Result = Arguments
End Sub

Private Sub bwConditionCalc_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwConditionCalc.RunWorkerCompleted
    Dim Arguments As New Hashtable
    Arguments = e.Result
    ' Check if missed calc Params can now be calculated/scaled
'The Error occurs here---------------------       
    If Not Arguments("VariablesCalculated") = 0 Then      
        Arguments("SchedCalculateData") = True
        Arguments("SchedConditionalCalc") = True
    End If
    prgFieldMaster.Value = 0
    lblStatus.Text = LabelString
    RunWorkSchedule(Arguments)
End Sub

修改
DeepCopy例程返回一个哈希表,并作为尝试修复程序放在那里。我仍然会在没有Deep Copy的情况下得到相同的错误,我将从引用的代码中删除它。

编辑 - e.error句柄
    Private Sub bwConditionCalc_RunWorkerCompleted(ByVal sender As Object,ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)处理bwConditionCalc.RunWorkerCompleted

    If (e.Error IsNot Nothing) Then
        MessageBox.Show(e.Error.Message)
    End If
    Dim Arguments As New Hashtable
    Arguments = e.Result
    ' Check if missed calc Params can now be calculated/scaled
    If Not Arguments("VariablesCalculated") = 0 Then
        Arguments("SchedCalculateData") = True
        Arguments("SchedConditionalCalc") = True
    End If
    prgFieldMaster.Value = 0
    lblStatus.Text = LabelString
    RunWorkSchedule(Arguments)
End Sub

编辑 - 工作人员安排

下面的子程序由一个启动子程序调用,该子程序创建原始Arguments Hashtable。在每个工人完成它之后,再在工人完成的rountine中再次调用该子工具。 我调用ImportcheckerImportDataCalculateData然后ConditionalCalc。{/ p>

    Public Sub RunWorkSchedule(ByVal Arguments As Hashtable)
    Me.Refresh()
    'Do nothing if worker is busy
    If bwCalcParam.IsBusy = True Or bwScaleData.IsBusy = True Or bwImportWriter.IsBusy = True Or bwColumnAdder.IsBusy = True Or bwConditionCalc.IsBusy = True Or bwDeleteParameterbyUpload.IsBusy Or bwDeletebyParameterOrCondition.IsBusy Or bwEventCalc.IsBusy Or bwImportChecker.IsBusy Or bwAddRawTables.IsBusy Then Exit Sub
    DataSchedRunning = True
    'Scheduled tasks:

    If Arguments("SchedUploadDelete") = True Then
        Arguments("SchedUploadDelete") = False
        lblStatus.Text = "Deleting Data by Upload"
        bwDeleteParameterbyUpload.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedDeletebyParameterOrCondition") = True Then
        Arguments("SchedDeletebyParameterOrCondition") = False
        lblStatus.Text = "Deleting Data by Parameter Or Condtion"
        bwDeletebyParameterOrCondition.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedAddRawTables") = True Then
        Arguments("SchedAddRawTables") = False
        lblStatus.Text = "Adding raw data tables to the Database..."
        bwAddRawTables.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedColumnAdder") = True Then
        Arguments("SchedColumnAdder") = False
        lblStatus.Text = "Adding Columns to the Database..."
        bwColumnAdder.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedImportChecker") = True Then
        Arguments("SchedImportChecker") = False
        lblStatus.Text = "Preparing to check Import Files..."
        bwImportChecker.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedImportData") = True Then
        Arguments("SchedImportData") = False
        lblStatus.Text = "Preparing to Import data..."
        bwImportWriter.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedCalculateData") = True Then
        Arguments("SchedCalculateData") = False
        lblStatus.Text = "Preparing to Calculate data..."
        bwCalcParam.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedConditionalCalc") = True Then
        Arguments("SchedConditionalCalc") = False
        lblStatus.Text = "Preparing to Calculate Conditional Data..."
        bwConditionCalc.RunWorkerAsync(Arguments)
        Exit Sub
    End If
    If Arguments("SchedScaleData") = True Then
        Arguments("SchedScaleData") = False
        lblStatus.Text = "Preparing to Scale Data..."
        bwScaleData.RunWorkerAsync(Arguments)
        Exit Sub
    End If

    If Arguments("SchedEventCalc") = True Then
        Arguments("SchedEventCalc") = False
        lblStatus.Text = "Preparing to Calculate Events"
        bwEventCalc.RunWorkerAsync(Arguments)
        Exit Sub
    End If
Finish:
    If SchedNewArgument = True Then
        timNextTask.Enabled = True
    End If
    lblStatus.Text = "Tasks Complete"
    StatusStrip1.Refresh()
    DataSchedRunning = False
End Sub

1 个答案:

答案 0 :(得分:2)

此代码中e.Result设置为Nothing的唯一方式是DeepCopy正在返回Nothing。这不是e.Result的问题。使用传入的参数调试DeepCopy方法,您将发现问题。