.net 后台工作程序未设置'e.result'`
概述:.net 后台工作程序未设置'e.result'`,.net,vb.net,backgroundworker,.net,Vb.net,Backgroundworker,概述: 我有几个后台工作人员用于将数据导入access数据库,然后执行一些计算。我使用e.Argument和e.Result在后台工作人员之间传递参数的hashtable。此哈希表包含有关要导入的文件的数据 问题: 在我的一个后台工作程序(流程中约有四分之一)上,我无法设置e.result,而是将其保持为nothing,这会导致工作程序完成的子例程出错。据我所知,这是我为其他员工编写的所有代码的副本,因此没有理由不设置此代码 代码: Private Sub bwConditionCalc_DoW
我有几个后台工作人员用于将数据导入access数据库,然后执行一些计算。我使用
e.Argument
和e.Result
在后台工作人员之间传递参数的hashtable
。此哈希表包含有关要导入的文件的数据
问题:在我的一个后台工作程序(流程中约有四分之一)上,我无法设置
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
例程返回一个哈希表,并作为一个尝试的修复程序放在那里。我仍然会在没有深度复制的情况下收到相同的错误,我将从引用的代码中删除到现在
编辑-e.错误
句柄私有子BWConditionalc_RunWorkerCompleted(ByVal发送方作为对象,ByVal e作为System.ComponentModel.RunWorkerCompletedEventArgs)处理BWConditionalc.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
编辑-工作人员时间表
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
下面的子例程由创建原始参数哈希表的启动子例程调用。在每个辅助进程完成后,它会在辅助进程完成的例程中再次调用此子进程。
在导致调用此操作的例程之前的周期,我正在调用Importchecker
,ImportData
,CalculateData
,然后调用ConditionalCalc
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
在此代码中,
e.Result
可以设置为Nothing
的唯一方法是DeepCopy
返回Nothing
。这不是e.Result
的问题。使用传入的参数调试DeepCopy
方法,您将发现问题。您忘记先检查e.Error。这是一个困难的要求,没有其他方法可以知道DoWork()事件处理程序因异常而提前终止。好吧,除了崩溃,你会从使用结果中得到什么。使用Debug+异常调试,勾选CLR异常的抛出复选框。@HansPassant,我已打开CLR异常的抛出,但仍然没有抛出任何内容。我还添加了一个e.error检查,该检查来自上面的示例,但它也是nothing
。我是否需要更改worker.dowork例程中的代码?非常感谢“我使用e.Argument和e.Result在后台工作人员之间传递了一个参数哈希表。”所以你要将它们链接在一起?“你能告诉我你是怎么做的吗?”懒鬼,请看上面……真是一团糟。就个人而言,我只使用一个BackgroundWorker()并使用其ReportProgress()方法,而不是RunWorkerCompleted()并将其“链接”在一起。您仍然可以将代码分解成不同的方法来保持整洁。然后,在DoWork()处理程序中只有一个“main”序列,依次调用每个步骤,然后调用ReportProgress()。遵循流程的逻辑和流程会容易得多。我还可以让你更容易发现问题所在……请看我刚才添加的编辑。DeepCopy不会更改哈希表,只是试图进行错误后修复。如果没有它,我仍然会得到相同的错误。@pezzz,那么这意味着在DoWork
其他地方会抛出异常。我猜,e.Argument
是null
因此,当您尝试在下一行访问Arguments
时,您会得到一个null引用异常。我想我需要检查代码的其余部分。