无法将参数传递给BackgroundWorker.RunWorkerAsync(System.MissingMemberException)

时间:2019-02-26 04:41:14

标签: vb.net asynchronous exception arguments backgroundworker

我正在创建一个数据库作业执行程序,该程序将允许用户自动从各种API接口检索数据集,并将其导入到我们的数据库中。

一旦在应用程序启动时加载了所有作业,后台工作人员engine_JobTrigger就会启动一个循环,该循环现在立即检查日期和时间,并将其与存储库中每个作业的NextExecutionTime进行比较。如果发生触发器,则后台线程将创建一个新的BackgroundWorker实例(需要此实例,以便可以触发其他作业来启动),并由DoWorkJob.ExecuteJob添加一个处理程序,然后开始工作程序执行:

'Class definition for Job Executor Engine
Public Class engine_JobExecutorArgs
        Public SourceJob As Job = Nothing
        Public ExecutedAs As String = ""
        Public ExecuteStartTime As DateTime = Nothing
End Class
.............................................

engine_JobTrigger.DoWork \|/
.............................................
While Not engine.Cancel = True
   Dim jobsToStart As New List(Of Integer)
   If JobController.Jobs.Count > 0 Then
       For Each process As Job In JobController.Jobs
           If process.Schedule.isNowNextExecutionTime(process.LastExecutionTime) And Not process.isExecuting Then jobsToStart.Add(process.JobID)
       Next
   End If

   If jobsToStart.Count > 0 Then
       For Each JobID As Integer In jobsToStart
           'Argument passed to BackgroundWorker
           Dim newJobExecutorArgs As New engine_JobExecutorArgs() With {
               .SourceJob = JobController.GetJob(CType(JobID, Integer)),
               .ExecutedAs = Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(),
               .ExecuteStartTime = Now
           }
           Dim newJobExecutor As New System.ComponentModel.BackgroundWorker()
           AddHandler newJobExecutor.DoWork, AddressOf newJobExecutorArgs.SourceJob.ExecuteJob
           AddHandler newJobExecutor.RunWorkerCompleted, AddressOf engine_JobExecutor_RunWorkerCompleted

           newJobExecutor.RunWorkerAsync(newJobExecutorArgs)
       Next
   Else
       Threading.Thread.Sleep(200)
   End If

End While

一切都很好,直到我进入Job.ExecuteJob函数并尝试声明传递给新创建的BackgroundWorker的参数为止:

Dim inputArgs As MainWindow.engine_JobExecutorArgs = DirectCast(e.Argument(0), MainWindow.engine_JobExecutorArgs)
System.MissingMemberException
  HResult=0x80131512
  Message=No default member found for type 'engine_JobExecutorArgs'.
  Source=Microsoft.VisualBasic
  StackTrace:
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.InternalLateIndexGet(Object Instance, Object[] Arguments, String[] ArgumentNames, Boolean ReportErrors, ResolutionFailure& Failure, Boolean[] CopyBack)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateInvokeDefault(Object Instance, Object[] Arguments, String[] ArgumentNames, Boolean ReportErrors, Boolean[] CopyBack)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateIndexGet(Object Instance, Object[] Arguments, String[] ArgumentNames)
   at Mastermind.Job.ExecuteJob(Object sender, DoWorkEventArgs e) in ...Mastermind\JobObject.vb:line 184
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

有人有什么建议吗?我很乐意继续将类对象用作我的BackgroundWorker的参数,稍后在我完成并需要实现新功能时会为我提供帮助。在这种情况下,如何将自定义类作为参数传递给'RunWorkerAsync`?谢谢您提前提出建议!

编辑:一旦发生错误,我就可以成功评估e.Argument(0)并通过IDE查看类中的值。

1 个答案:

答案 0 :(得分:0)

这是关于什么的

formatted_desc_split = ['Akshay Godase is from pune', 'Amar:Satara', 'Sandesh:Solapur', 'Mahesh:Nagpur', 'Prashant:Indapur']

for each_split_data in formatted_desc_split:
    if ":" not in each_split_data:
        ...

您为什么要使用Dim inputArgs As MainWindow.engine_JobExecutorArgs = DirectCast(e.Argument(0), MainWindow.engine_JobExecutorArgs) e.Argument(0)只是一个e.Argument引用,它引用您传入的对象。您传入的对象的类型为Object

engine_JobExecutorArgs

可以为该类型建立索引吗,即它是列表类型吗?不,不是,那么您希望对它建立索引是什么? Dim newJobExecutorArgs As New engine_JobExecutorArgs() '... newJobExecutor.RunWorkerAsync(newJobExecutorArgs) 指向该类型的对象,因此您应该将其强制转换为该类型:

e.Argument

没有索引可以完成,这就是错误消息告诉您的内容。您正在尝试为后期绑定索引编制索引,这表明您没有此类成员。如果您有Dim inputArgs As MainWindow.engine_JobExecutorArgs = DirectCast(e.Argument, MainWindow.engine_JobExecutorArgs) ,则会被告知不允许索引Option Strict On,因为e.Argument不是可索引的类型。