【发布时间】:2016-05-19 18:06:54
【问题描述】:
我有一个构建报告文档的程序,我想将 例程在后台工作人员的“DoWork”处理程序下构建报告。报告的初始部分已启动,但是,一旦我在组合框中引用选定的项目,它就会停止执行?
这是我的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ProgressBar1.Visible = True
Application.EnableVisualStyles()
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.MarqueeAnimationSpeed = 10
Dim x As New Thread(AddressOf buildReport)
x.Start()
MessageBox.Show("Build Complete")
ProgressBar1.Visible = False
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
'builds the report
Public Sub buildReport()
Dim app As word.Application = New word.Application
Dim document As word.Document
Dim today As String()
app.Visible = True
document = app.Documents.Add("K:\ETL Test Files\" & mycallerPreview.previewInst.txtYear.Text & "\" & mycallerPreview.previewInst.txtVendor.Text & "\" & mycallerPreview.previewInst.txtReport.Text & "\Test Report\Report Data\ReportTemplate.doc") 'open up template
'document.Styles.Add("Contents1")
'document.Styles.Add("Contents2")
'document.Styles.Add("Contents3")
'add info to pre-made bookmarks
today = Date.Today.ToString.Split(" ")
document.Bookmarks("Date").Range.Text = today(0).ToString
document.Bookmarks("Date1").Range.Text = today(0).ToString
document.Bookmarks("Date2").Range.Text = today(0).ToString
document.Bookmarks("Date3").Range.Text = today(0).ToString
document.Bookmarks("Approver").Range.Text = mycallerPreview.mycallerSelect2.txtChecked.Text.ToString
document.Bookmarks("Number2").Range.Text = mycallerPreview.mycallerSelect2.txtReportNumber.Text.ToString
document.Bookmarks("Number1").Range.Text = mycallerPreview.mycallerSelect2.txtReportNumber.Text.ToString
document.Bookmarks("Vendor").Range.Text = mycallerPreview.previewInst.txtVendor.Text.ToString
document.Bookmarks("Test1").Range.Text = mycallerPreview.mycallerSelect2.txtReportTitle.Text.ToString
document.Bookmarks("TestTitle").Range.Text = mycallerPreview.mycallerSelect2.txtReportTitle.Text.ToString
If mycallerPreview.mycallerSelect2.cmbName.SelectedIndex <> -1 Then
document.Bookmarks("Writer").Range.Text = mycallerPreview.mycallerSelect2.cmbName.SelectedItem.ToString
document.Bookmarks("Reviewer").Range.Text = mycallerPreview.mycallerSelect2.cmbName.SelectedItem.ToString
End If
If mycallerPreview.mycallerSelect2.cmbQuote.SelectedIndex <> -1 Then
document.Bookmarks("Quote").Range.Text = mycallerPreview.mycallerSelect2.cmbQuote.SelectedItem.ToString
End If
我的 word 文档中的所有书签都被填充,直到到达“DoWork”处理程序底部的组合框引用。有什么建议吗?
更新:
按照建议,我尝试了线程同步...
Dim x As New Thread(AddressOf buildReport)
x.Start()
这并没有解决我的问题,但给了我以下异常:
跨线程操作无效:控件“cmbName”从创建它的线程以外的线程访问。
修订:
'garbage collects and initializes progress bar to default values
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'create list of objects to pass through ThreadStart Method
Dim list As New List(Of Object)
If mycallerPreview.mycallerSelect2.cmbName.SelectedIndex <> -1 Then
list.Add(mycallerPreview.mycallerSelect2.cmbName.SelectedItem.ToString)
End If
If mycallerPreview.mycallerSelect2.cmbQuote.SelectedIndex <> -1 Then
list.Add(mycallerPreview.mycallerSelect2.cmbQuote.SelectedItem.ToString)
End If
list.Add(ProgressBar1)
Dim x As New Thread(AddressOf buildReport)
x.Start(list)
MessageBox.Show("Build Complete")
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
'builds the report
Public Sub buildReport(list_temp As Object)
Dim progress As New ProgressBar
progress = list_temp(2)
progress.Visible = True
Application.EnableVisualStyles()
progress.Style = ProgressBarStyle.Marquee
progress.MarqueeAnimationSpeed = 10
Dim list As List(Of Object) = list_temp
Dim app As word.Application = New word.Application
Dim document As word.Document
Dim today As String()
app.Visible = True
document = app.Documents.Add("K:\ETL Test Files\" & mycallerPreview.previewInst.txtYear.Text & "\" & mycallerPreview.previewInst.txtVendor.Text & "\" & mycallerPreview.previewInst.txtReport.Text & "\Test Report\Report Data\ReportTemplate.doc") 'open up template
'add info to pre-made bookmarks
today = Date.Today.ToString.Split(" ")
document.Bookmarks("Date").Range.Text = today(0).ToString
document.Bookmarks("Date1").Range.Text = today(0).ToString
document.Bookmarks("Date2").Range.Text = today(0).ToString
document.Bookmarks("Date3").Range.Text = today(0).ToString
document.Bookmarks("Approver").Range.Text = mycallerPreview.mycallerSelect2.txtChecked.Text.ToString
document.Bookmarks("Number2").Range.Text = mycallerPreview.mycallerSelect2.txtReportNumber.Text.ToString
document.Bookmarks("Number1").Range.Text = mycallerPreview.mycallerSelect2.txtReportNumber.Text.ToString
document.Bookmarks("Vendor").Range.Text = mycallerPreview.previewInst.txtVendor.Text.ToString
document.Bookmarks("Test1").Range.Text = mycallerPreview.mycallerSelect2.txtReportTitle.Text.ToString
document.Bookmarks("TestTitle").Range.Text = mycallerPreview.mycallerSelect2.txtReportTitle.Text.ToString
document.Bookmarks("Writer").Range.Text = list(0).ToString
document.Bookmarks("Reviewer").Range.Text = list(0).ToString
document.Bookmarks("Quote").Range.Text = list(1).ToString
【问题讨论】:
-
您遇到异常了吗?
SelectedItem可能不会导致 NRE - 但您没有检查。与其引用不同线程上的控件,不如使用DoWorkEventArgs将所需的任何数据传递给工作人员 -
不抛出异常。 & 我会试试看谢谢!
-
BackgroundWorker 不错,但我更喜欢在设置异步方法时使用 Thread 和 ThreadStart。
-
试过了。在我的问题中更新了它。
-
刚刚将 [ComboBox] [Selected Items] 作为对象列表传递给线程启动方法。谢谢你的帮助!!
标签: .net vb.net multithreading combobox backgroundworker