第二次执行后台工作程序时出现 InvalidOperationException
我正在 vb2010 Express 中使用 wpf 和 vb.net 制作一个应用程序,并且遇到一个让我发疯的问题。
我有一个每分钟执行一次后台工作程序的计时器:
Private Sub timer_Tick() Handles timer.Tick
If Not bworker.IsBusy Then
bworker.RunWorkerAsync()
End If
End Sub
后台工作程序正在使用 xmlelement 和 xmldataprovider:
Private Sub bworker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bworker.DoWork
Dim source As XmlDataProvider = Application.Current.Resources("r1")
Dim sometext As String = "something"
Dim elemento As XmlElement = source.Document.CreateElement("elemento")
elemento.InnerText = sometext
e.Result = elemento
End Sub
然后我在 RunWorkerComplete 上使用 e.Result 将 XmlElement 添加到源。我第一次执行后台工作程序时它工作得很好,但是当计时器第二次调用它时,它会在“elemento.InnerText = sometext”行中抛出 InvalidOperationException ,为什么会这样以及如何解决它?
I'm making an app using wpf and vb.net in vb2010 express and having a problem that is driving me crazy.
I have a timer that execute a backgroundworker every minute:
Private Sub timer_Tick() Handles timer.Tick
If Not bworker.IsBusy Then
bworker.RunWorkerAsync()
End If
End Sub
The backgroundworker is working with xmlelement and xmldataprovider:
Private Sub bworker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bworker.DoWork
Dim source As XmlDataProvider = Application.Current.Resources("r1")
Dim sometext As String = "something"
Dim elemento As XmlElement = source.Document.CreateElement("elemento")
elemento.InnerText = sometext
e.Result = elemento
End Sub
Then I'm using e.Result on RunWorkerComplete to add the XmlElement to source. It works perfect the first time I execute the backgroundworker, but when the timer calls it the second time it throws a InvalidOperationException in the line "elemento.InnerText = sometext" why is that and how can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用
DispatcherTimer
而不是上面的普通Timer
吗?Can you try using
DispatcherTimer
instead of normalTimer
above?